假设我有以下几点:
数据帧:
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位于同一ownerId的当前startId和当前endId之间的所有“value”列求和
输出应为:
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等,但我无法得到我需要的
你有什么建议吗
您可以在单个指令中执行:
如果数据集不太大,可以使用selfjoin:
输出:
相关问题 更多 >
编程相关推荐