gpt4 book ai didi

python - 即使使用 inplace=True,pandas replace 也不会替换值

转载 作者:太空狗 更新时间:2023-10-30 02:50:45 27 4
gpt4 key购买 nike

我的数据是这样的。如果 'no_of_children' 不是 nan,我想用 'Married' 替换 marital_status 'Missing'

>cust_data_df[['marital_status','no_of_children']]
>

marital_status no_of_children

0 Married NaN
1 Married NaN
2 Missing 1
3 Missing 2
4 Single NaN
5 Single NaN
6 Married NaN
7 Single NaN
8 Married NaN
9 Married NaN
10 Single NaN

这是我尝试过的:

cust_data_df.loc[cust_data_df['no_of_children'].notna()==True, 'marital_status'].replace({'Missing':'Married'},inplace=True)

但这并没有做任何事情。

最佳答案

为避免 chained assignments 分配回替换值:

m = cust_data_df['no_of_children'].notna()
d = {'Missing':'Married'}
cust_data_df.loc[m, 'marital_status'] = cust_data_df.loc[m, 'marital_status'].replace(d)

如果需要设置所有值:

cust_data_df.loc[m, 'marital_status'] = 'Married'

编辑:

感谢@Quickbeam2k1 的解释:

cust_data_df.loc[cust_data_df['no_of_children'].notna()==True, 'marital_status'] is just a new object which has no reference. Replacing there, will leave the original object unchanged

关于python - 即使使用 inplace=True,pandas replace 也不会替换值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58152897/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com