我有一张这样的桌子:
position class supl score1 score2
1 3 2 15 18
2 2 5 18 13
3 4 5 13 18
4 6 9 15 12
. . . . .
. . . . .
100 5 9 12 09我尝试在位置1-5的基础上分组,然后在同一窗口中添加分数。我试着做窗口,但困惑于如何在同一窗口中添加分数。我正在尝试获得如下输出:
position score1 score2
1-5 72 71
6-10 68 71
. . .
. . .
96-100 x x我如何才能实现聚类数据并累积相应分数的目标。请分享你的想法,我将非常感谢你们所有人。
发布于 2021-07-05 14:25:37
IIUC,这是通过pd.cut / groupby的一种方式
df = df.groupby(pd.cut(df.position ,bins=[i for i in range(0, df.position.max() + 1, 5)])).agg({'score1': sum , 'score2': sum}).reset_index()另一种选择-(如果你已经按顺序排列了所有的位置):
df1 = df.groupby(df.position // 5).agg({'score1': sum , 'score2': sum}).reset_index()
df1.position = df1.position.astype(str) + '-' + df1.position.add(4).astype(str)https://stackoverflow.com/questions/68251453
复制相似问题