1 回答
TA贡献2041条经验 获得超4个赞
创建一个辅助列,您将对其进行累计总和。需要在每个组内移动,因为您的计数仅包括之前的获胜值:
df['to_sum'] = (df.deal_status == 'won').astype(int)
df['cum_count'] = (df.groupby('industry')
.apply(lambda x: x.to_sum.shift(1).cumsum()).fillna(0)
.reset_index(0, drop=True))
输出df:
time company industry deal_status to_sum cum_count
0 1 ciaA x won 1 0.0
1 2 ciaB y lost 0 0.0
2 3 ciaA x won 1 1.0
3 4 ciaC x won 1 2.0
4 5 ciaA x lost 0 3.0
5 6 ciaD y won 1 0.0
6 7 ciaE x lost 0 3.0
添加回答
举报