Replacing values in multiple specific columns of a Dataframe Replacing values in multiple specific columns of a Dataframe pandas pandas

Replacing values in multiple specific columns of a Dataframe


You cannnot use inplace=True, because subset returns a Series which may have its data as a view. Modifying it in place doesn't ALWAYS propogate it back to the parent object. That's why SettingWithCopyWarning is possible there (or raise if you set the option). You should never do this, nor is their ever a reason to do so.

df[['Score2', 'Score3']] = df[['Score2', 'Score3']].replace(-999, np.nan)print (df)   Score1  Score2  Score30      42     NaN     2.01      52     NaN     2.02    -999     NaN     NaN3      24     2.0     2.04      73     1.0     NaN


Use

In [282]: df.replace({'Score2': -999, 'Score3': -999}, np.nan)Out[282]:   Score1  Score2  Score30      42     NaN     2.01      52     NaN     2.02    -999     NaN     NaN3      24     2.0     2.04      73     1.0     NaN