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
添加回答
舉報