I have what I believe to be some anomalies in my dataset so I want to change all negative values in a specific column of a pandas dataframe to Null/NaN
This is the code I have so far:
df_1[df_1['Column D']<0] = np.nan
But what it is doing is changing all the values in the row that have a negative in column D to NaN when I only need the negative values in Column D changed
I have also played around using df.loc, df.replace and df.mask but couldn't find a way that worked.
CodePudding user response:
two options:
a)
df_1.loc[df_1['Column D'] < 0, 'Column D'] = np.nan
b)
df_1['Column D'] = df_1['Column D'].mask(df_1['Column D'] < 0, np.nan)
they both should work correctly, not sure which one is faster, it's a trivial question , you have chance to test them it would be cool to have it on file.