基于日期和组的条件

时间:2018-04-10 15:25:48

标签: python pandas conditional

            A    B           C   D
0  2002-01-13  Dan  2002-01-15  10
1  2002-01-13  Dan  2002-01-25  24
2  2002-01-13  Vic  2002-01-17  14
3  2002-01-13  Vic  2002-01-03  18
4  2002-01-28  Mel  2002-02-08  37
5  2002-01-28  Mel  2002-02-06  29
6  2002-01-28  Mel  2002-02-10  20
7  2002-01-28  Rob  2002-02-12  30
8  2002-01-28  Rob  2002-02-01  47

我想为每个df['E']组创建一个新的B列,其中包含下一个条件:

  • E = D值,其中A日期距离C日期最近10天。
  • 如果距离C的10天内有两个A个日期(2002-01-28 Mel的情况),E将是平均值这些同期D值。

输出应为:

            A    B           C   D   E
0  2002-01-13  Dan  2002-01-15  10  24
1  2002-01-13  Dan  2002-01-25  24  24
2  2002-01-13  Vic  2002-01-17  14  14
3  2002-01-13  Vic  2002-01-03  18  14
4  2002-01-28  Mel  2002-02-08  37  33
5  2002-01-28  Mel  2002-02-06  29  33
6  2002-01-28  Mel  2002-02-10  20  33
7  2002-01-28  Rob  2002-02-12  30  30 
8  2002-01-28  Rob  2002-02-01  47  30

1 个答案:

答案 0 :(得分:2)

好的,好像你需要

df['E']=abs((df.C-df.A).dt.days-10)# get the days different 
df['E']=df.B.map(df.loc[df.E==df.groupby('B').E.transform('min')].groupby('B').D.mean())# find the min value for the different , and get the mean 
df
Out[106]: 
           A    B          C   D   E
0 2002-01-13  Dan 2002-01-15  10  24
1 2002-01-13  Dan 2002-01-25  24  24
2 2002-01-13  Vic 2002-01-17  14  14
3 2002-01-13  Vic 2002-01-03  18  14
4 2002-01-28  Mel 2002-02-08  37  33
5 2002-01-28  Mel 2002-02-06  29  33
6 2002-01-28  Mel 2002-02-10  20  33
7 2002-01-28  Rob 2002-02-12  30  30
8 2002-01-28  Rob 2002-02-01  47  30