一旦达到阈值,累积熊猫列“重置”

时间:2021-01-04 09:55:33

标签: python pandas threshold cumsum

我面临以下数据集的问题:

item                  price       
1                     1706
2                     210
3                     1664
4                     103
5                     103
6                     314
7                     1664
8                     57
9                     140
10                    1628
11                    688
12                    180
13                    604
14                    86
15                    180
16                    86
17                    1616
18                    832
19                    1038
20                    57
21                    2343
22                    151
23                    328
24                    328
25                    57
26                    86
27                    1706
28                    604
29                    609
30                    86
31                    0
32                    57
33                    302
34                    328

我想要一个累积总和列,每次达到阈值时“重置”(阅读不要超过它,只要不超过最后一个累积总和数与阈值之间有很大差距就可以了它)。

我尝试了以下代码:

threshold = (7.17*1728)*0.75  #this is equal to 9292.32
df['cumsum'] = df.groupby((df['price'].cumsum()) // threshold)['price'].cumsum()

输出如下:

item                  price             cumsum    
1                     1706              1706
2                     210               1916
3                     1664              3580
4                     103               3683
5                     103               3786
6                     314               4100
7                     1664              5764
8                     57                5821
9                     140               5961
10                    1628              7589
11                    688               8277
12                    180               8757
13                    604               9061
14                    86                9147
15                    180               9327 #exceeds threshold
16                    86                9413 #
17                    1616              1616
18                    832               2448
19                    1038              3486
20                    57                3543
21                    2343              5886
22                    151               6037
23                    328               6365
24                    328               6693
25                    57                6750
26                    86                6836
27                    1706              8542
28                    604               9146
29                    609               9755 #exceeds threshold same below
30                    86                9841 #
31                    0                 9841 #
32                    57                9898 #
33                    302               10200 #
34                    328               328

我的预期结果如下(例如第一部分):

item                  price             cumsum    
1                     1706              1706
2                     210               1916
3                     1664              3580
4                     103               3683
5                     103               3786
6                     314               4100
7                     1664              5764
8                     57                5821
9                     140               5961
10                    1628              7589
11                    688               8277
12                    180               8757
13                    604               9061
14                    86                9147
15                    180               180 #
16                    86                266 #

我需要改变什么才能得到这个结果?我也希望能解释一下为什么上面的代码不起作用。

提前致谢。

2 个答案:

答案 0 :(得分:0)

也许成本很高,但它可以工作......

threshold = (7.17*1728)*0.75  #this is equal to 9292.32
df['cumsum'] = df['price'].cumsum()

# handle the cumsum which is gt threshold by loops
n = 1
while True:
    print(n)
    cond = df['cumsum'].ge(threshold)
    if cond.sum():
        df.loc[cond, 'cumsum'] = df.loc[cond, 'price'].cumsum()
    else:
        break
    n += 1

答案 1 :(得分:0)

感谢您的所有回复和反馈。

我继续使用以下代码解决了我的问题:

ls = []
cumsum = 0
lastreset = 0
for _, row in df.iterrows():
    if cumsum + row.price <= threshold:
        cumsum += row.price
    else:
        last_reset = cumsum
        cumsum = row.price
    ls.append(cumsum)

df['cumsum'] = ls
相关问题