ValueError:索引器与 DataFrame 不兼容

问题描述

我试图根据另一个数据框中的一行覆盖一个数据框中的一行。对替代方法持开放态度!

这个循环运行了 15 次,然后出错。奇怪的。同一种循环在同一个函数中运行得很好。

for row in rows_unique:
    print(row in df_a.index) # all True...
    print(row in df_b.index) # all True...
    
    df_a.loc[row] = df_b.loc[row]

这是跟踪的尾部

~/.pyenv/versions/3.8.7/envs/jupyterlab3/lib/python3.8/site-packages/pandas/core/indexing.py in __setitem__(self,key,value)
    690 
    691         iloc = self if self.name == "iloc" else self.obj.iloc
--> 692         iloc._setitem_with_indexer(indexer,value,self.name)
    693 
    694     def _validate_key(self,axis: int):

~/.pyenv/versions/3.8.7/envs/jupyterlab3/lib/python3.8/site-packages/pandas/core/indexing.py in _setitem_with_indexer(self,indexer,name)
   1635             self._setitem_with_indexer_split_path(indexer,name)
   1636         else:
-> 1637             self._setitem_single_block(indexer,name)
   1638 
   1639     def _setitem_with_indexer_split_path(self,name: str):

~/.pyenv/versions/3.8.7/envs/jupyterlab3/lib/python3.8/site-packages/pandas/core/indexing.py in _setitem_single_block(self,name)
   1852 
   1853         elif isinstance(value,ABCDataFrame) and name != "iloc":
-> 1854             value = self._align_frame(indexer,value)
   1855 
   1856         # check for chained assignment

~/.pyenv/versions/3.8.7/envs/jupyterlab3/lib/python3.8/site-packages/pandas/core/indexing.py in _align_frame(self,df)
   2080             return val
   2081 
-> 2082         raise ValueError("Incompatible indexer with DataFrame")
   2083 
   2084 

ValueError: Incompatible indexer with DataFrame

Trace 非常注重 iloc。

熊猫 v1.2.3

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)