Given Dataset: New dataset:
ball score ball score current_score total_score
0.1 0 0.1 0 0 13
0.2 1 0.2 1 1 13
0.3 4 0.3 4 5 13
1.1 0 1.1 0 5 13
1.2 2 1.2 2 7 13
2.1 6 2.1 6 13 13
0.1 1 0.1 1 1 5
1.1 3 1.1 3 4 5
1.2 1 1.2 1 5 5
0.1 6 0.1 6 6 7
0.2 0 0.2 0 6 7
0.3 1 0.3 1 7 7
我想创建2列,而current_score=current_score+score,直到ball=0.1,对于total_score,它返回current_score的最大值,直到ball=0.1(递归(我的代码:
df = pd.read_csv("score.csv")
df['total_score'] = df.groupby(df['ball'].ne(df['ball'].shift())
.cumsum())['score'].transform('sum')
我是熊猫的新手。请帮帮我。
我们可以首先用cumsum
找到的0.1序列进行分组,然后current_score
是分组的累积和。对于total_score
,我们transform
,current_score
和max
具有相同的组:
gr = df.groupby(df.ball.eq(0.1).cumsum())
df["current_score"] = gr.score.cumsum()
df["total_score"] = gr.current_score.transform("max")
获取
>>> df
ball score current_score total_score
0 0.1 0 0 13
1 0.2 1 1 13
2 0.3 4 5 13
3 1.1 0 5 13
4 1.2 2 7 13
5 2.1 6 13 13
6 0.1 1 1 5
7 1.1 3 4 5
8 1.2 1 5 5
9 0.1 6 6 7
10 0.2 0 6 7
11 0.3 1 7 7