简体   繁体   中英

Iterate through dataframe to extract delta of a particular time period

I have a file, df, that I wish to take the delta of every 7 day period and reflect the timestamp for that particular period

df:

Date          Value
10/15/2020    75
10/14/2020    70
10/13/2020    65
10/12/2020    60
10/11/2020    55
10/10/2020    50
10/9/2020     45
10/8/2020     40
10/7/2020     35
10/6/2020     30
10/5/2020     25
10/4/2020     20
10/3/2020     15
10/2/2020     10
10/1/2020     5

Desired Output:

10/15/2020 to 10/9/2020 is 7 days with the delta being: 75 - 45 = 30 10/9/2020 timestamp would be: 30 and so on

Date          Value

10/9/2020     30
10/2/2020     30

This is what I am doing:

df= df['Delta']=df.iloc[:,6].sub(df.iloc[:,0]),Date=pd.Series
(pd.date_range(pd.Timestamp('2020-10- 
15'), 
periods=7, freq='7d')))[['Delta','Date']]

I am also thinking I may be able to do this:

Edit I updated callDate to Date

for row in df.itertuples():
  Date = datetime.strptime(row.Date, "%m/%d/%y  %I:%M %p")
  previousRecord = df['Date'].shift(-6).strptime(row.Date, "%m/%d/%y  %I:%M 
  %p")
  Delta = Date - previousRecord

Any suggestion is appreciated

Don't iterate through the dataframe. You can use a merge :

(df.merge(df.assign(Date=df['Date'] - pd.to_timedelta('6D')),
         on='Date')
   .assign(Value = lambda x: x['Value_y']-x['Value_x'])
   [['Date','Value']]
)

Output:

        Date  Value
0 2020-10-09     30
1 2020-10-08     30
2 2020-10-07     30
3 2020-10-06     30
4 2020-10-05     30
5 2020-10-04     30
6 2020-10-03     30
7 2020-10-02     30
8 2020-10-01     30

The last block of code you wrote is the way I would do it. Only problem is in Delta = Date - previousRecord , there is nothing called Date here. You should instead be accessing the value associated with callDate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM