pandas 终极版4:DataFrame统计、合并、分组操作

来源:互联网 发布:python中get函数 编辑:程序博客网 时间:2024/06/09 17:10

padndas提供了丰富的统计、合并、分组、缺失值等操作函数。

1.统计函数

  1. df.count() #非空元素计算
  2. df.min() #最小值
  3. df.max() #最大值
  4. df.idxmin() #最小值的位置,类似于R中的which.min函数
  5. df.idxmax() #最大值的位置,类似于R中的which.max函数
  6. df.quantile(0.1) #10%分位数
  7. df.sum() #求和
  8. df.mean() #均值
  9. df.median() #中位数
  10. df.mode() #众数
  11. df.var() #方差
  12. df.std() #标准差
  13. df.mad() #平均绝对偏差
  14. df.skew() #偏度
  15. df.kurt() #峰度
  16. df.describe() #一次性输出多个描述性统计指标

2.分组group by

对于”group by”操作,我们通常是指以下一个或多个操作步骤:

Splitting:按照一些规则将数据分为不同的组;

Applying:对于每组数据分别执行一个函数;

Combining:将结果组合到一个数据结构中;


(1)按sex分组,显示每个分组第一个【first()】并求和[sum()]。最后一个【last()】

In [22]: dfOut[22]:        B         C         D  height  name sex  weight0    one  1.077877 -0.316243     160   jia   F      501    two  0.843750 -1.133218     180    yi   M      602  three  0.422464 -0.034921     120  bing   F      453    two -0.381719 -0.763121     200  ding   F      784    one  1.085882 -0.281334     186    wu   M      685   thee  0.619578  2.333398     170    ji   M      766    two  1.447991 -0.579820     165  geng   F      587  three  0.031053  0.168869     155   xin   M      39In [23]: df.groupby('sex').first()Out[23]:        B         C         D  height name  weightsex                                              F    one  1.077877 -0.316243     160  jia      50M    two  0.843750 -1.133218     180   yi      60In [24]: df.groupby('sex').sum()Out[24]:             C         D  height  weightsex                                    F    2.566612 -1.694106     645     231M    2.580262  1.087714     691     243

(2)通过多个列进行分组形成一个层次索引,然后执行函数:

In [25]: df.groupby(['sex','B']).last()Out[25]:                   C         D  height  name  weightsex B                                              F   one    1.077877 -0.316243     160   jia      50    three  0.422464 -0.034921     120  bing      45    two    1.447991 -0.579820     165  geng      58M   one    1.085882 -0.281334     186    wu      68    thee   0.619578  2.333398     170    ji      76    three  0.031053  0.168869     155   xin      39    two    0.843750 -1.133218     180    yi      60In [26]: df.groupby(['sex','B']).sum()Out[26]:                   C         D  height  weightsex B                                        F   one    1.077877 -0.316243     160      50    three  0.422464 -0.034921     120      45    two    1.066271 -1.342941     365     136M   one    1.085882 -0.281334     186      68    thee   0.619578  2.333398     170      76    three  0.031053  0.168869     155      39    two    0.843750 -1.133218     180      60

3.合并

Pandas提供了大量的方法能够轻松的对SeriesDataFramePanel对象进行各种符合各种逻辑关系的合并操作

(1)Concat

   result = pd.concat(objs, axis=1, join='inner')

objs: series,dataframe或者是panel构成的序列lsit 
axis: 需要合并链接的轴,0是行,1是列 ,当axis = 1的时候,concat就是行对齐,然后将不同列名称的两张表合并
join:连接的方式 inner,或者outer ,如果为’inner’得到的是两表的交集,如果是outer,得到的是两表的并集。

 

随机生产两个df1和df2

In [27]: df1 = pd.DataFrame(np.random.randint(100,size=(2,2)))In [28]: df2 = pd.DataFrame(np.random.randint(100,size=(3,3)))In [29]: df1Out[29]:     0   10  56  271  54  32In [30]: df2Out[30]:     0   1   20  54  62   51  88  91  432  25  34  40
pd.concat([df1,df2]),pd.concat([df1,df2],axis=1)
In [31]: pd.concat([df1,df2])Out[31]:     0   1     20  56  27   NaN1  54  32   NaN0  54  62   5.01  88  91  43.02  25  34  40.0In [32]: pd.concat([df1,df2],axis=1)Out[32]:       0     1   0   1   20  56.0  27.0  54  62   51  54.0  32.0  88  91  432   NaN   NaN  25  34  40
inner’得到的是两表的交集,如果是outer,得到的是两表的并集。
In [36]: pd.concat([df1,df2],join='inner')Out[36]:     0   10  56  271  54  320  54  621  88  912  25  34In [37]: pd.concat([df1,df2],join='outer')Out[37]:     0   1     20  56  27   NaN1  54  32   NaN0  54  62   5.01  88  91  43.02  25  34  40.0
(2)Append:无视index的concat
如果两个表的index都没有实际含义,使用ignore_index参数,置true,合并的两个表就睡根据列字段对齐,然后合并。最后再重新整理一个新的index。 

In [49]: df1Out[49]:     0   10  56  271  54  32In [50]: s = pd.Series(np.random.randint(100,size=5))In [51]: sOut[51]: 0     31    722    643    574     5dtype: int64In [52]: df1.append(s,ignore_index=True)Out[52]:     0   1     2     3    40  56  27   NaN   NaN  NaN1  54  32   NaN   NaN  NaN2   3  72  64.0  57.0  5.0
In [56]: df2Out[56]:     0   1   20  54  62   51  88  91  432  25  34  40In [57]: df1.append(df2,ignore_index=True)Out[57]:     0   1     20  56  27   NaN1  54  32   NaN2  54  62   5.03  88  91  43.04  25  34  40.0
(3)merge的参数

on:列名,join用来对齐的那一列的名字,用到这个参数的时候一定要保证左表和右表用来对齐的那一列都有相同的列名。
left_on:左表对齐的列,可以是列名,也可以是和dataframe同样长度的arrays。
right_on:右表对齐的列,可以是列名,也可以是和dataframe同样长度的arrays。
left_index/ right_index: 如果是True的haunted以index作为对齐的key
how:数据融合的方法。(left,right,inner,outer),默认是inner
sort:根据dataframe合并的keys按字典顺序排序,默认是,如果置false可以提高表现。

In [21]: leftOut[21]:     A   B key1 key20  A0  B0   K0   K01  A1  B1   K0   K12  A2  B2   K1   K03  A3  B3   K2   K1In [22]: rightOut[22]:     C   D key1 key20  C0  D0   K0   K01  C1  D1   K1   K02  C2  D2   K1   K03  C3  D3   K2   K0In [23]: result = pd.merge(left, right, on=['key1', 'key2'])In [24]: resultOut[24]:     A   B key1 key2   C   D0  A0  B0   K0   K0  C0  D01  A2  B2   K1   K0  C1  D12  A2  B2   K1   K0  C2  D2