goupby

>>> df = pd.DataFrame({'A':['a','b','a','a','c','b','c','b','a'],
           'B':[2,2,4,5,7,8,9,3,5],
           'C':[143,675,233,127,43,55,735,672,433]})
>>> df
   A  B    C
0  a  2  143
1  b  2  675
2  a  4  233
3  a  5  127
4  c  7   43
5  b  8   55
6  c  9  735
7  b  3  672
8  a  5  433
>>> df.groupby('A')

>>> df.groupby('A').mean()
          B           C
A                      
a  4.000000  234.000000
b  4.333333  467.333333
c  8.000000  389.000000
>>> df.groupby(['A','B']).mean()
       C
A B     
a 2  143
  4  233
  5  280
b 2  675
  3  672
  8   55
c 7   43
  9  735
>>> goup = df.groupby('A')
>>> goup['B'].mean()
A
a    4.000000
b    4.333333
c    8.000000
Name: B, dtype: float64
>>> goup['C'].sum()
A
a     936
b    1402
c     778
Name: C, dtype: int64
>>> goup.agg({'B':'mean','C':'sum'})
          B     C
A                
a  4.000000   936
b  4.333333  1402
c  8.000000   778
>>> 

你可能感兴趣的:(goupby)