如何在DataFrame中根据某列分组,然后对不同的列进行不同的聚合计算?
1. 先生成一个Dataframe
import pandas as pd
data = {"class":[1,2,1,2],"student":[1,2,3,4],"math":[99,98,60,66],"chinese":[60,80,85,77],"english":[77,68,99,88]}
df = pd.DataFrame(data)
print(df)
class student math chinese english 0 1 1 99 60 77 1 2 2 98 80 68 2 1 3 60 85 99 3 2 4 66 77 88
2. 根据班级class分组,分别计算math,chinese,english的平均分,总分和最高分
df.groupby("class").agg({"math":"mean","chinese":"sum","english":"max"})
| class | math | chinese | english |
|---|

本文介绍了如何在Python的DataFrame中根据某列进行分组,并对不同列执行平均分、总分和最高分的聚合计算,以班级class为例进行演示。

6455

被折叠的 条评论
为什么被折叠?



