0

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

Geralt
  • 3
  • 2
  • s indicated in [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") a Good Question requires that you include the simplest data and code which reproduces your problem in a form that a respondent can copy and paste into their environment and a sample of how that data should be converted. While your question includes input data. Please edit your question to provide the essential elemnents. – itprorh66 Jan 12 '22 at 00:10
  • there is no `ghi_derived >2000` in your sample data here. Please include a sample that illustrates your problem. –  Jan 12 '22 at 00:45
  • @itprorh66 Thank you for the comments. I did edit it , hope it's understandable now – Geralt Jan 12 '22 at 01:03
  • @enke I did include a clearer sample, – Geralt Jan 12 '22 at 01:42

0 Answers0