Python 熊猫替换特定列上的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34653215/
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
Pandas replacing values on specific columns
提问by mcocdawc
I am aware of these two similar questions:
我知道这两个类似的问题:
Pandas: Replacing column values in dataframe
I used a different approach for substituting values from which I think it should be the cleanest one. But it does not work. I know how to work around it, but I would like to understand why it does not work:
我使用了一种不同的方法来替换我认为应该是最干净的值。但它不起作用。我知道如何解决它,但我想了解为什么它不起作用:
In [108]: df=pd.DataFrame([[1, 2, 8],[3, 4, 8], [5, 1, 8]], columns=['A', 'B', 'C'])
In [109]: df
Out[109]:
A B C
0 1 2 8
1 3 4 8
2 5 1 8
In [110]: df.loc[:, ['A', 'B']].replace([1, 3, 2], [3, 6, 7], inplace=True)
In [111]: df
Out[111]:
A B C
0 1 2 8
1 3 4 8
2 5 1 8
In [112]: df.loc[:, 'A'].replace([1, 3, 2], [3, 6, 7], inplace=True)
In [113]: df
Out[113]:
A B C
0 3 2 8
1 6 4 8
2 5 1 8
If I slice only one column In [112]
it works different to slicing several columns In [110]
. As I understand the .loc
method it returns a view and not a copy. In my logic this means that making an inplace change on the slice should change the whole DataFrame. This is what happens at line In [110]
.
如果我只切片一列,In [112]
它的工作方式与切片几列不同In [110]
。据我了解,.loc
它返回一个视图而不是副本。在我的逻辑中,这意味着对切片进行就地更改应该会更改整个 DataFrame。这就是 line 发生的事情In [110]
。
采纳答案by mcocdawc
Here is the answer by one of the developers: https://github.com/pydata/pandas/issues/11984
以下是其中一位开发人员的回答:https: //github.com/pydata/pandas/issues/11984
This should ideally show a SettingWithCopyWarning, but I think this is quite difficult to detect.
理想情况下,这应该显示 SettingWithCopyWarning,但我认为这很难检测到。
You should NEVER do this type of chained inplace setting. It is simply bad practice.
你永远不应该做这种类型的链接就地设置。这只是不好的做法。
idiomatic is:
惯用语是:
In [7]: df[['A','B']] = df[['A','B']].replace([1, 3, 2], [3, 6, 7])
In [8]: df
Out[8]:
A B C
0 3 7 8
1 6 4 8
2 5 3 8
(you can do with df.loc[:,['A','B']]
as well, but more clear as above.
(你也可以这样做df.loc[:,['A','B']]
,但更清楚如上。