1. Dataframe分组用groupby("列名")或者groupby(["列名1","列名2"])
import pandas as pd df = pd.DataFrame({'性别' : ['男', '女', '男', '女', '男', '女', '男', '男'], '成绩' : ['优秀', '优秀', '及格', '差', '及格', '及格', '优秀', '差'], '年龄' : [15,14,15,12,13,14,15,16]}) df
count=df.groupby(["性别","年龄"]).count() count
count=df.groupby("性别").count() count
2. Series分组用groupby(Series)
import pandas as pd ds = pd.Series({'小明' : '男', '小张' : '女', '小赵' : '男'}) GroupBy=ds.groupby(ds) GroupBy.describe()