pandas中groupby

import numpy as np
import pandas as pd
df=pd.DataFrame({'key1':['a','a','b','b','a'],
                 'key2':['one','two','one','two','one'],
                 'data1':[1,4,7,3,9],
                 'data2':[3,8,5,1,6]})
In[4]: df
Out[4]: 
   data1  data2 key1 key2
0      1      3    a  one
1      4      8    a  two
2      7      5    b  one
3      3      1    b  two
4      9      6    a  one
grouped=df['data1'].groupby(df['key1'])
In[5]: grouped.mean()
Out[5]: 
key1
a    4.666667
b    5.000000
Name: data1, dtype: float64
grou=df[['data1','data2']].groupby(df['key1'])
In[6]: grou.mean()
Out[6]: 
         data1     data2
key1                    
a     4.666667  5.666667
b     5.000000  3.000000
gr=df['data1'].groupby([df['key1'],df['key2']])
In[7]: gr.mean()
Out[7]: 
key1  key2
a     one     5
      two     4
b     one     7
      two     3
Name: data1, dtype: int64
states=np.array(['pp','cc','cc','pp','pp'])
years=np.array([2005,2005,2006,2005,2006])
In[8]: df['data1'].groupby([states,years]).mean()
Out[8]: 
cc  2005    4
    2006    7
pp  2005    2
    2006    9
Name: data1, dtype: int64
In[9]; df.groupby(['key1','key2']).size()
Out[9]: 
key1  key2
a     one     2
      two     1
b     one     1
      two     1
dtype: int64


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值