如何在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 | m |
---|