【问题标题】:Conditional cumsum and reset to 0条件 cumsum 并重置为 0
【发布时间】:2021-11-20 15:56:44
【问题描述】:

我阅读了这篇文章,但并不完全适用于我的案例pandas-resetting cumsum at a specific number

我对“累积”列的预期输出:

comment         count   pre_cnt_diff    cumulation
auto 1          10      0               0
auto 2          30      20              20
auto 3          70      40              60
auto 4          120     50              110
auto 5          120     0               110
auto 6          130     10              120
auto 7          150     20              140
manual_input_1  150     0               0
auto 8          200     50              50
auto 9          230     30              80
manual_input_2  230     0               0

我在“累积”列上的当前输出:

comment         count   pre_cnt_diff    cumulation
auto 1          10      0               0
auto 2          30      20              20
auto 3          70      40              60
auto 4          120     50              110
auto 5          120     0               0
auto 6          130     10              10
auto 7          150     20              30
manual_input_1  150     0               0
auto 8          200     50              50
auto 9          230     30              80
manual_input_2  230     0               0

这是我的代码:

import pandas as pd

d = {'comment': ['auto 1', 'auto 2', 'auto 3', 'auto 4', 'auto 5', 'auto 6', 
'auto 7', 'manual input 1', 'auto 8', 'auto 9', 'manual input 2'],
'count': [10,30,70,120,120,130,150,150,200,230,230]}
df = pd.DataFrame(data=d)

df['pre_cnt_diff'] = df['count'].diff().fillna(0)
g = df.pre_cnt_diff.eq(0).cumsum().fillna(0)
df['cumulation'] = df.groupby(g).pre_cnt_diff.cumsum()

当注释具有字符串“manual_input”的特殊模式并继续从最后一个 0 值累加时,我希望列“累积”重置为 0。我认为这必须使用正则表达式来搜索我的评论栏并添加 if-else 语句。如果有人可以协助修改我的代码,将不胜感激。

【问题讨论】:

    标签: python pandas cumsum


    【解决方案1】:

    创建自定义组:

    grps = df['comment'].str.contains(r'^manual input').cumsum()
    
    df['cumulation'] = df.groupby(grps)['pre_cont_diff'].cumsum()
    

    输出:

    >>> df
               comment  count  pre_cont_diff  cumulation
    0           auto 1     10            0.0         0.0
    1           auto 2     30           20.0        20.0
    2           auto 3     70           40.0        60.0
    3           auto 4    120           50.0       110.0
    4           auto 5    120            0.0       110.0
    5           auto 6    130           10.0       120.0
    6           auto 7    150           20.0       140.0
    7   manual input 1    150            0.0         0.0
    8           auto 8    200           50.0        50.0
    9           auto 9    230           30.0        80.0
    10  manual input 2    230            0.0         0.0
    

    详情:

    >>> pd.concat([df['comment'], grps], axis=1)
               comment  comment
    0           auto 1        0
    1           auto 2        0
    2           auto 3        0
    3           auto 4        0
    4           auto 5        0
    5           auto 6        0
    6           auto 7        0
    7   manual input 1        1
    8           auto 8        1
    9           auto 9        1
    10  manual input 2        2
    

    【讨论】:

    • 对了,请您解释一下为什么在搜索字符串中需要“^”符号?
    • @Kelvin 这是一个正则表达式。它将仅匹配以“手动输入”开头的值。
    • @user17242583 是对的。您可以查看文档here
    猜你喜欢
    • 1970-01-01
    • 2017-02-06
    • 2016-04-04
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多