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