Python / Pandas - ValueError:与Series不兼容的索引器

时间:2017-06-28 01:40:01

标签: python pandas

我有一个数据框:

df:

        A      B 
id
 3   'Yes'    23
 5   'Yes'    67
 6    'No'    56
 8    'No'    23

我有另一个数据框:

calc:
       A    B
id   
 3   'No'   4 

我想用calc值更新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

如果尝试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

任何人都可以帮助启发我的旅程吗?

1 个答案:

答案 0 :(得分:5)

您可以使用update方法直接覆盖它。

df.update(calc)