Pandas replacing values on specific columns

后端 未结 2 685
感动是毒
感动是毒 2021-02-15 11:41

I am aware of these two similar questions:

Pandas replace values

Pandas: Replacing column values in dataframe

I used a different approach for substitutin

2条回答
  •  执念已碎
    2021-02-15 11:49

    Here is the answer by one of the developers: https://github.com/pydata/pandas/issues/11984

    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.

    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.

提交回复
热议问题