从连续两行获取数学值

时间:2018-10-17 13:19:49

标签: python pandas

这是我拥有的数据框

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)

如果我们打印数据框,则可以看到ID = A2的价格从7/31到8/11丢失(查看StartDate和EndDate)。我们遇到了ID = A3

的情况

我想做的是,找出按ID分组的StartDate-EndDate(属于前几列)。

我的输出应类似于:

 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

缺少NoOfDays由StartDate-EndDate(上一行)针对每个ID(按每个ID分组)计算

1 个答案:

答案 0 :(得分:1)

使用shift从上一行获取EndDate,取其差值,然后在dt中使用具有days属性的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