- DataFrame.groupby(key, as_index=False)
- key:分组的列数据,可以多个
案例:不同颜色的不同笔的价格数据
col =pd.DataFrame({'color': ['white','red','green','red','green'], 'object': ['pen','pencil','pencil','ashtray','pen'],'price1':[5.56,4.20,1.30,0.56,2.75],'price2':[4.75,4.12,1.60,0.75,3.15]})
cool
#先分组再取列
col.groupby(['color'])['price1'].count()
#不作为索引
col.groupby(['color'],as_index=False)['price1'].count()
#先取列再分组
col['price1'].groupby(col['color']).count() # 注意调原来col的color这列