for 循环遍历分组
import pandas as pd
import numpy as np
df=pd.DataFrame({
'name':['BOSS','Lilei','Lilei','Han','BOSS','BOSS','Han','BOSS'],
'Year':[2016,2016,2016,2016,2017,2017,2017,2017],
'Salary':[999999,20000,25000,3000,9999999,999999,3500,999999],
'Bonus':[100000,20000,20000,5000,200000,300000,3000,400000]
})
print(df)
查看分组情况
for name,group in group_by_name:
print(name)
print(group)
交叉表的使用
df = pd.DataFrame({'Age': np.random.randint(20, 70, 100),
'Sex': np.random.choice(['M', 'F'], 100),
})
print(df)
age_groups = pd.cut(df['Age'], bins=[19,40,65,100])
age_groups
pd.crosstab(age_groups, df['Sex'])