I have the following dataframe:
import pandas as pd
import numpy as np
raw_data = {
'Score1': [42, 52, -999, 24, 73],
'Score2': [-999, -999, -999, 2, 1],
'Score3': [2, 2, -999, 2, -999]}
df = pd.DataFrame(raw_data, columns = ['Score1', 'Score2', 'Score3'])
and I want to replace the -999's with NaN only in columns Score2 and Score3, leaving column Score1 unchanged. I want to reference the columns to be modified by name and they might not be consecutive.
I have tried things like:
df.loc[:,('Score2', 'Score3')].replace(-999, np.nan, inplace=True)
df
but this does not work, I assume because it is operating on the copy. Is there a way of doing this in one statement?
I looked at Pandas replacing values on specific columns but found it quite confusing, so felt a simpler example would help.