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

2024-10-01 22:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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 #

为了得到这个结果,我需要改变什么?另外,我希望您能解释一下为什么上面的代码不起作用

先谢谢你


Tags: 数据代码dfthresholdis阈值数字equal
2条回答

也许它花费很多,但它可以工作

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

感谢您的回复和反馈

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

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

相关问题 更多 >

    热门问题