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
列,其中包含下一个条件:
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
答案 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