【发布时间】:2019-05-14 19:20:46
【问题描述】:
我有代码行
df = df.groupby(by=['col_A','col_B'])['float_col_c']
df.loc[:,'amount_cumulative'] = df.apply(lambda x: x.cumsum())
哪个会引发警告:
/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:362: 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)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py:543: 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[] 来修复它,但在这种情况下,警告似乎是指另一个问题。我知道我可以压制警告,但我宁愿理解我在使用 Pandas 语法时遇到的问题。非常感谢任何有关如何更正此语法的建议。
【问题讨论】: