熊猫如何使用GroupBy分组

  介绍

这篇文章主要介绍熊猫如何使用GroupBy分组,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

GroupBy对象

 import  pandas  as  pd
  import  numpy  as  np
  df =, pd.DataFrame({& # 39;一个# 39;,:,(& # 39;foo # 39;,, & # 39;酒吧# 39;,,& # 39;foo # 39;,, & # 39;酒吧,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,& # 39;foo # 39;,, & # 39;酒吧# 39;,,& # 39;foo # 39;,, & # 39; foo # 39;),,,,,,,,,,,,,,,,,,,,,,,, & # 39; b # 39;,:,(& # 39;一个# 39;,,& # 39;一个# 39;,,& # 39;两个# 39;,,& # 39;三,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,& # 39;两个# 39;,,& # 39;两个# 39;,,& # 39;一个# 39;,,& # 39;3 # 39;),,,,,,,,,,,,,,,,,,,,,,,,& # 39;c # 39;,:, np.random.randn (8),,,,,,,,,,,,,,,,,,,,,,,, & # 39; d # 39;,:, np.random.randn (8)})
  gb.groupby(& # 39;一个# 39;)
  打印(df.groupby(& # 39;一个# 39;))
  & lt; pandas.core.groupby.DataFrameGroupBy  object  at  0 x00000000042f3470>
  [26]:拷贝,gb。你们;TAB>
  gb.agg ,,,,,,, gb.boxplot ,,, gb.cummin ,,,,,,, gb.describe  gb.filter ,,,, gb.get_group , gb.height ,,,, gb.last ,,,,,, gb.median ,,,, gb.ngroups ,,, gb.plot ,,,,,, gb.rank ,,,,,, gb.std ,,,,,, gb.transform
  gb.aggregate , gb.count ,,,,, gb.cumprod ,,, gb.dtype ,,,,, gb.first ,,,,, gb.groups ,,,, gb.hist ,,,,,, gb.max ,,,,,,, gb.min ,,,,,,, gb.nth ,,,,,,, gb.prod ,,,,,,,,, gb.resample  gb.sum ,,,,,, gb.var
  gb.apply ,,,,, gb.cummax ,,,, gb.cumsum ,,,, gb.fillna ,,,, gb.gender ,,,, gb.head ,,,,,, gb.indices ,,, gb.mean ,,,,,, gb.name ,,,,,, gb.ohlc ,,,,,,,,, gb.quantile  gb.size ,,,,,, gb.tail ,,,,,, gb.weight 

分组迭代遍历组

<>之前拷贝[41]:,grouped =, df.groupby(& # 39;一个# 39;)      [42]:拷贝,for 名字,,group 拷贝分组:   ,,....:,,,,,,,,印刷(名字)   ,,....:,,,,,,,,印刷(集团)   ,,,....:   酒吧   ,,,,A ,,,,, B ,,,,,,,, C ,,,,,,,, D1 , bar ,,, one  -0.042379, -0.0893293,, bar , three  -0.009920, -0.9458675,, bar ,,, two , 0.495767, 1.956030 foo   ,,,,A ,,,,, B ,,,,,,,, C ,,,,,,,, D0 , foo ,,, one  -0.919854, -1.1313452,, foo ,,, two , 1.247642,, 0.3378634,, foo ,,, two , 0.290213, -0.9321326,, foo ,,, one , 0.362949,, 0.0175877,, foo , three , 1.548106, -0.016692

获得一个分组get_group

<>之前拷贝[44]:,grouped.get_group(& # 39;酒吧# 39;)[44]:,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

熊猫如何使用GroupBy分组