防止SettingWithCopy警告,正确使用Pandas数据帧的视图/副本

时间:2017-02-08 00:50:58

标签: python python-3.x pandas

在更改从另一个数据帧切片创建的数据帧时,是否应始终使用.copy()方法显式生成副本?否则,我收到一个SettingWithCopy警告。但是,在这种情况下,它没有带来任何麻烦;原始数据框保持不变。

>>> import pandas as pd
>>> df = pd.DataFrame([[6,3,2],[4,3,2],[5,4,2],[4,3,5]], columns=['a', 'b', 'c'])
>>> df
   a  b  c
0  6  3  2
1  4  3  2
2  5  4  2
3  4  3  5
>>> df2 = df.loc[df.a<6, :]
>>> df2.loc[df2.b==3, 'b'] = 99
/usr/lib/python3/dist-packages/pandas/core/indexing.py:117: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self._setitem_with_indexer(indexer, value)
__main__:1: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
>>> df
   a  b  c
0  6  3  2
1  4  3  2
2  5  4  2
3  4  3  5
>>> df2
   a   b  c
1  4  99  2
2  5   4  2
3  4  99  5

或者,如果我执行以下操作,则不会收到任何警告。

>>> df2 = df.loc[df.a<6, :].copy()
>>> df2.loc[df2.b==3, 'b'] = 99
>>> df2
   a   b  c
1  4  99  2
2  5   4  2
3  4  99  5
>>> df
   a  b  c
0  6  3  2
1  4  3  2
2  5  4  2
3  4  3  5

后者更好吗? (因此我没有得到任何警告)。凭什么?是因为我确信df2是副本,因此无法改变原始数据帧df?

0 个答案:

没有答案