问题描述
我知道这两个类似的问题:
I am aware of these two similar questions:
我使用了另一种替代值的方法,我认为这应该是最干净的方法.但这是行不通的.我知道如何解决它,但我想了解为什么它不起作用:
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
如果仅切一列In [112]
,则其工作方式与切几列In [110]
的工作原理不同.据我了解.loc
方法,它返回一个视图,而不是副本.在我的逻辑中,这意味着在切片上进行就地更改应该更改整个DataFrame.这就是在行In [110]
发生的情况.
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]
.
推荐答案
以下是开发人员之一的回答: https://github.com/pydata/pandas/issues/11984
Here is the answer by one of the developers: https://github.com/pydata/pandas/issues/11984
理想情况下,这应该显示SettingWithCopyWarning,但是我认为这很难检测到.
This should ideally show a SettingWithCopyWarning, but I think this is quite difficult to detect.
绝对不要执行这种类型的链式就地设置.这简直是不好的做法.
You should NEVER do this type of chained inplace setting. It is simply bad practice.
惯用语是:
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
(您也可以使用df.loc[:,['A','B']]
,但如上所述更加清晰.
(you can do with df.loc[:,['A','B']]
as well, but more clear as above.
这篇关于 pandas 替换特定列上的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!