首页 > 解决方案 > Get math value from two consecutive row

问题描述

Here is the dataframe I have

import pandas as pd
import datetime
data = [['A1','String01',45,datetime.date(2018,1,1),datetime.date(2018,3,1)],
['A1','String02',46,datetime.date(2018,3,1),datetime.date(2018,4,29)],
['A1','String03',48,datetime.date(2018,4,29),datetime.date(2018,6,30)],
['A1','String04',51,datetime.date(2018,6,30),datetime.date(2018,12,31)],
['A2','String11',32,datetime.date(2018,1,1),datetime.date(2018,6,1)],
['A2','String12',33,datetime.date(2018,6,1),datetime.date(2018,7,30)],
['A2','String13',54,datetime.date(2018,8,11),datetime.date(2018,12,31)],
['A3','String21',45,datetime.date(2018,1,1),datetime.date(2018,6,1)],
['A3','String22',47,datetime.date(2018,7,1),datetime.date(2018,12,31)],]

cols = ['ID','SomeValue','Price','StartDate','EndDate']

df = pd.DataFrame(data,columns=cols)
print(df)

If we printed the dataframe, we can see Price for ID=A2 is missing from 7/31 to 8/11 (looking at the StartDate and EndDate). We have a similar situation with ID=A3

What I would want to do it, find out StartDate - EndDate (of previous columns) grouped by ID.

My output should be something like:

 ID SomeValue  Price   StartDate     EndDate  NoOfDaysMissing
0  A1  String01     45  2018-01-01  2018-03-01              NaN
1  A1  String02     46  2018-03-01  2018-04-29              0.0
2  A1  String03     48  2018-04-29  2018-06-30              0.0
3  A1  String04     51  2018-06-30  2018-12-31              0.0
4  A2  String11     32  2018-01-01  2018-06-01              NaN
5  A2  String12     33  2018-06-01  2018-07-30              0.0
6  A2  String13     54  2018-08-11  2018-12-31             12.0
7  A3  String21     45  2018-01-01  2018-06-01              NaN
8  A3  String22     47  2018-07-01  2018-12-31             30.0

where NoOfDays missing is calculated by StartDate - EndDate(of previous row) for each ID (grouped by each ID)

标签: pythonpandas

解决方案


使用,shift从上一行获取 EndDate,取差,然后在 a 中使用dtdays属性的访问器groupby

df[['StartDate','EndDate']] = df[['StartDate','EndDate']].apply(pd.to_datetime)
df['NoOfDaysMissing'] = df.groupby('ID', group_keys=False)\
                          .apply(lambda x: (x['StartDate'] - x['EndDate'].shift()).dt.days)
df

输出:

   ID SomeValue  Price  StartDate    EndDate  NoOfDaysMissing
0  A1  String01     45 2018-01-01 2018-03-01              NaN
1  A1  String02     46 2018-03-01 2018-04-29              0.0
2  A1  String03     48 2018-04-29 2018-06-30              0.0
3  A1  String04     51 2018-06-30 2018-12-31              0.0
4  A2  String11     32 2018-01-01 2018-06-01              NaN
5  A2  String12     33 2018-06-01 2018-07-30              0.0
6  A2  String13     54 2018-08-11 2018-12-31             12.0
7  A3  String21     45 2018-01-01 2018-06-01              NaN
8  A3  String22     47 2018-07-01 2018-12-31             30.0

推荐阅读