2017-08-07 36 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