我有一个数据框:
df:
A B
id
3 'Yes' 23
5 'Yes' 67
6 'No' 56
8 'No' 23
我还有一个数据框:
calc:
A B
id
3 'No' 4
我想用计算值更新df。我试着用以下方法:
tgsm.loc[i]=calc
但是,这不起作用。我一直得到以下错误:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/pandas/core/indexing.py", line 693, in _align_series
raise ValueError('Incompatible indexer with Series')
ValueError: Incompatible indexer with Series
如果try
tgsm.loc[i]=calc[i]
,则会出现另一个错误:File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368)
File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322)
KeyError: 3
有人能帮助我启发我的旅程吗?
最佳答案
您可以使用update方法直接就地覆盖它。
df.update(calc)
关于python - Python/Pandas-ValueError:与Series不兼容的索引器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44792362/