4

My data looks like this. I would like to replace marital_status 'Missing' with 'Married' if 'no_of_children' is not 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

This is what I tried:

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

But this is not doing anything.

Tomasz Bartkowiak
  • 12,154
  • 4
  • 57
  • 62

1 Answers1

4

Assign back replaced values for avoid 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)

If need set all values:

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

EDIT:

Thanks @Quickbeam2k1 for explanation:

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

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 3
    Maybe some more explanation: `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 – Quickbeam2k1 Sep 29 '19 at 06:53