Python 在来自 DataFrame 的切片副本上设置值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31468176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting values on a copy of a slice from a DataFrame
提问by vincent
I have a small dataframe, say this one :
我有一个小数据框,说这个:
Mass32 Mass44
12 0.576703 0.496159
13 0.576658 0.495832
14 0.576703 0.495398
15 0.576587 0.494786
16 0.576616 0.494473
...
I would like to have a rolling mean of column Mass32
, so I do this:
我想要一个滚动平均值 column Mass32
,所以我这样做:
x['Mass32s'] = pandas.rolling_mean(x.Mass32, 5).shift(-2)
It works as in I have a new column named Mass32s
which contains what I expect it to contain but I also get the warning message:
它的工作原理就像我有一个名为的新列Mass32s
,其中包含我希望它包含的内容,但我也收到警告消息:
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 the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
试图在来自 DataFrame 的切片副本上设置值。尝试使用 .loc[row_indexer,col_indexer] = value 代替
请参阅文档中的警告:http: //pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
I'm wondering if there's a better way to do it, notably to avoid getting this warning message.
我想知道是否有更好的方法来做到这一点,特别是避免收到此警告消息。
采纳答案by firelynx
This warning comes because your dataframe x
is a copy of a slice. This is not easy to know why, but it has something to do with how you have come to the current state of it.
出现此警告是因为您的数据帧x
是切片的副本。这并不容易知道为什么,但这与您如何达到当前状态有关。
You can either create a proper dataframe
out of x by doing
您可以dataframe
通过执行从 x创建一个适当的
x = x.copy()
This will remove the warning, but it is not the proper way
这将消除警告,但这不是正确的方法
You should be using the DataFrame.loc
method, as the warning suggests, like this:
DataFrame.loc
正如警告所暗示的那样,您应该使用该方法,如下所示:
x.loc[:,'Mass32s'] = pandas.rolling_mean(x.Mass32, 5).shift(-2)