有条件的 cumsum 并重置为 0
Conditional cumsum and reset to 0
我读过这个 post 但不完全适用于我的情况
我在“累积”列上的预期输出:
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”的特殊模式时,“cumulation”列重置为 0,并从最后的 0 值继续累积。我认为这必须使用正则表达式来搜索我的评论栏并添加 if-else 语句。如果有人可以帮助修改我的代码,将不胜感激。
创建自定义组:
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
我读过这个 post 但不完全适用于我的情况
我在“累积”列上的预期输出:
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”的特殊模式时,“cumulation”列重置为 0,并从最后的 0 值继续累积。我认为这必须使用正则表达式来搜索我的评论栏并添加 if-else 语句。如果有人可以帮助修改我的代码,将不胜感激。
创建自定义组:
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