0

I'm trying to replace a value in my Pandas dataframe when a certain condition is met. I've nearly got it and this is what I have currently:

df.loc[df['change daily'] < -5000] = 99999

The condition works great - it is able to identify a row where the 'change daily' value is less than 5000. However, this causes the entire row to be replaced with 99999.

10  2020-08-04         36675  ...      1    Tuesday
11       99999         99999  ...  99999      99999
12  2020-08-06         36502  ...      3   Thursday

How do I make it so only the 'change daily' is changed? Removing the row isn't an option since I still need to use numbers from the other columns.

Also while I have y'all here, is there a way to make it so this value is a null value? I was going to set it to a super high value and then replace all values that are equal to 99999 with null, but I'm sure there's a way to set it to null without the additional step.

Thanks :)

Phanster
  • 65
  • 6

1 Answers1

1
import numpy as np
df.loc[df['change daily'] < -5000].loc[:,"change daily"]=np.nan

Or Alternatively,

import numpy as np
indices=df.loc[df['change daily'] < -5000].index
df.loc[indices, "change daily"]=np.nan

Or Alternatively

df.loc[df['change daily'] < -5000, 'change daily'] = np.nan
Anant Kumar
  • 611
  • 5
  • 20