即使在使用.loc之后,也要设置withwithwarning错误

时间:2017-08-07 03:14:41

标签: python pandas

我收到此错误:

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

我正在使用最新的pandas(0.20.3)和python(3.6)

1 个答案:

答案 0 :(得分:1)

它告诉您正在修改您可能要修改的数据框的副本,而不是数据框本身。在这种情况下,group实际上是.groupby()生成器创建的另一个数据框。

您的代码正在离开results,并改为修改group。这可能不是你打算做的,这就是警告出现的原因。如果是,并且您想要禁用警告,则可以执行pd.set_option('SettingWithCopyWarning', None)