考虑到我有以下几点:
Dataframe:
id enddate startdate ownerId value
1 2019-10-05 2019-10-05 10 105
2 2019-10-06 2019-10-05 10 240
3 2019-10-07 2019-10-05 10 420
4 2019-10-08 2019-10-08 10 470
5 2019-10-01 2019-10-01 11 320
6 2019-10-02 2019-10-01 11 18
7 2019-10-10 2019-10-10 12 50
8 2019-10-12 2019-10-10 12 412
9 2019-10-14 2019-10-10 12 398
10 2019-10-15 2019-10-12 12 320我想要做的是,将endId位于当前startId和当前endId之间的所有“值”列之和为同一个ownerId。
产出应是:
id enddate startdate ownerId value output
1 2019-10-05 2019-10-05 10 105 105 # Nothing between 2019-10-05 and 2019-10-05
2 2019-10-06 2019-10-05 10 240 345 # Found 1 record (with id 1)
3 2019-10-07 2019-10-05 10 420 765 # Found 2 records (with id 1 and 2)
4 2019-10-08 2019-10-08 10 470 470 # Nothing else between 2019-10-08 and 2019-10-08
5 2019-10-01 2019-10-01 11 320 320 # Reset because Owner is different
6 2019-10-02 2019-10-01 11 18 338 # Found 1 record (with id 5)
7 2019-10-10 2019-10-10 12 50 50 # ...
8 2019-10-12 2019-10-10 12 412 462
9 2019-10-14 2019-10-10 12 398 860
10 2019-10-15 2019-10-12 12 320 1130 # Found 3 records between 2019-10-12 and 2019-10-15 (with id 8, 9 and 10)我试图使用groupby.sum等,但我无法得到我所需要的.
你对怎么做有什么建议吗?
发布于 2019-12-12 17:15:00
您可以在单个指令中完成此操作:
df['output'] = df.apply(lambda row:
df[df.ownerId.eq(row.ownerId) & df.enddate.between(row.startdate, row.enddate)]
.value.sum(), axis=1)发布于 2019-12-12 17:16:28
如果数据集不太大,这里有一种使用自连接的方法:
df[['startdate','enddate']] = df[['startdate','enddate']].apply(pd.to_datetime)
df['output'] = (df.merge(df, on='ownerId', suffixes=('','_y'))
.query('startdate <= enddate_y <= enddate')
.groupby('id')['value_y']
.sum()
.to_numpy())
print(df)输出:
id enddate startdate ownerId value output
0 1 2019-10-05 2019-10-05 10 105 105
1 2 2019-10-06 2019-10-05 10 240 345
2 3 2019-10-07 2019-10-05 10 420 765
3 4 2019-10-08 2019-10-08 10 470 470
4 5 2019-10-01 2019-10-01 11 320 320
5 6 2019-10-02 2019-10-01 11 18 338
6 7 2019-10-10 2019-10-10 12 50 50
7 8 2019-10-12 2019-10-10 12 412 462
8 9 2019-10-14 2019-10-10 12 398 860
9 10 2019-10-15 2019-10-12 12 320 1130https://stackoverflow.com/questions/59309456
复制相似问题