2017-08-07 47 views
1

後settingwithcopywarning的錯誤,我得到這個錯誤:獲取即使使用的.loc

C:\Users\rt\Anaconda3\lib\site-packages\pandas\core\indexing.py:337: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame. 
Try using .loc[row_indexer,col_indexer] = value instead 

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
    self.obj[key] = _infer_fill_value(value) 
C:\Users\rt\Anaconda3\lib\site-packages\pandas\core\indexing.py:517: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame. 
Try using .loc[row_indexer,col_indexer] = value instead 

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy 
    self.obj[item] = s 

不過,我已經使用的.loc已經所以不知道爲什麼這個錯誤是發生。代碼大綱是這樣的:

for name, group in results.groupby(): 
    for idx, row in group.iterrows(): 
     group.loc[idx, 'area'] = 10.0 

我使用最新的大熊貓(0.20.3)和Python(3.6)

+0

你可以添加結果df嗎? – Dark

回答

1

它告訴你,你正在修改你可能是數據幀的副本打算修改,而不是數據框本身。在這種情況下,group實際上是由.groupby()生成器創建的另一個數據幀。

您的代碼將保持不變,並且修改group而不是results。這可能不是你打算做的,這就是警告出現的原因。如果是,並且您想禁用該警告,則可以執行pd.set_option('SettingWithCopyWarning', None)

+0

我確實打算修改並使用'group',這樣就可以了:)謝謝! – user308827