我的数据如下所示。如果'Missing'
不是'Married'
,我想用'no_of_children'
替换marital_status nan
。
>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)
但这没有做任何事情。
答案 0 :(得分:4)
为避免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()==真的,'marital_status'] 只是一个没有引用的新对象。在那里替换,将保持原始对象不变