0
我想设置一个列的值的基础上另一列值的熊猫数据框一定的值,选择大熊猫数据帧的头几行与列
df2.loc[df2['col1',len] == val, 'col2'] = df1['col2']
上面的代码工作正常,然而,现在的问题是,我想设置值仅适用于第几行,类似下面:
len1 = len(df1.index)
df2.loc[df2['col1',len1] == val, 'col2'] = df1['col2']
但我得到以下错误:
Traceback (most recent call last): File "...\lib\site-packages\pandas\indexes\base.py", line 1945, in get_loc return self._engine.get_loc(key) 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)
任何帮助将不胜感激。
但它不会更改df2内col2的值。 – Annie
改变它,现在应该工作 –
http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy – Annie