简体   繁体   中英

cumulative sum base of condition pandas

my data set

innings matchid over_count  runs_total_inover
1       1000887      1               1.0
1       1000887      2               1.0
1       1000887      50              3.0
2       1000887      1               6.0
2       1000887      50              2.0

i want like that

innings matchid over_count  runs_total_inover     sum
1       1000887      1               1.0          1.0  
1       1000887      2               1.0          2.0
1       1000887      50              3.0          5.0 
2       1000887      1               6.0          6.0 
2       1000887      50              2.0          8.0

IIUC, you can try groupby and cumsum

df['sum'] = df.groupby('innings')['runs_total_inover'].cumsum()
print(df)

   innings  matchid  over_count  runs_total_inover  sum
0        1  1000887           1                1.0  1.0
1        1  1000887           2                1.0  2.0
2        1  1000887          50                3.0  5.0
3        2  1000887           1                6.0  6.0
4        2  1000887          50                2.0  8.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM