简体   繁体   English

条件熊猫的累积和基数

[英]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 IIUC,你可以试试 groupby 和 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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