1

I would like to replace the nan values from two columns in my dataframe with zero. I can do this for one column at a time, but not for the columns simultaneously. I would think this is something that is possible.

This is working:

train_data['AMT_REQ_CREDIT_BUREAU_YEAR'].fillna(0, inplace=True)
train_data['DAYS_LAST_PHONE_CHANGE'].fillna(0, inplace=True)

However, if I try this, there are no errors, but it is not replacing the nan values:

cols = ["AMT_REQ_CREDIT_BUREAU_YEAR","DAYS_LAST_PHONE_CHANGE"]
train_data[cols].replace(0, np.nan, inplace=True)

I have also tried:

train_data[cols].fillna(0, inplace=True)

without success.

  • Try `train_data[[cold]].fillna...` – Umar.H Oct 31 '19 at 13:17
  • Try using `loc`to avoid replacing values on a *view* of the DataFrame: `train_data.loc[:, cols].fillna(0, inplace=True)` – jfaccioni Oct 31 '19 at 13:18
  • I couldn't get either of the suggestions above to work, however the following worked (from the link referenced by jezrael): `train_data.fillna({'AMT_REQ_CREDIT_BUREAU_YEAR':0, 'DAYS_LAST_PHONE_CHANGE':0}, inplace=True)` – Bernard Esterhuyse Nov 04 '19 at 12:59

0 Answers0