I'm trying to replace values based on condition with the day before value
my code:
import pandas as pd
import random
d = {
'year' : [2000,2000,2000,2001,2001,2001],
'month' : [1,1,1,2,2,2],
'day' : [1,1,1,2,2,2],
'ghi': [random.randint(700,1000) for i in range(6)],
'location' : ['Jeddah','Dammam','Taif','Jeddah','Dammam','Taif']
}
df = pd.DataFrame(data=d)
df['date'] = pd.to_datetime(df.loc[:,['year', 'month', 'day']])
df = df.sort_values('date').reset_index(drop=True)
df.set_index('date', inplace=True)
df.drop(['year', 'month', 'day'], axis = 1, inplace=True)
Which generate the following dataframe:
ghi location
date
2000-01-01 800 Jeddah
2000-01-01 700 Dammam
2000-01-01 700 Taif
2001-02-02 950 Jeddah
2001-02-02 990 Dammam
2001-02-02 750 Taif
Let's say if ghi > 900
I will change it with the day before for the same location
I tried this to get the day before for the records meeting the condition, but I can't find way to get the results for the same location only
x = df[df['ghi'] > 900]
day_before = x.index - pd.Timedelta('1 Day')
My desired output :
ghi location
date
2000-01-01 800 Jeddah
2000-01-01 700 Dammam
2000-01-01 700 Taif
2001-02-02 800 Jeddah
2001-02-02 700 Dammam
2001-02-02 750 Taif
Thank you in advance