美文网首页
3、pandas数据重构

3、pandas数据重构

作者: thelong的学习日记 | 来源:发表于2020-08-23 21:24 被阅读0次

1、使用concat做数据合并

1.1、横向合并

list_up = [text_left_up,text_right_up]
result_up = pd.concat(list_up,axis=1)

1.2、纵向合并

result = pd.concat([result_up,result_down])  #默认axis=0

2、使用group对数据进行聚合及运算

一图看懂group机制

image.png

分组求平均值

#以sex分组,输出Fare的值
df  = text['Fare'].groupby(text['Sex'])  
means = df.mean()      #运算
--------------------------------------
Sex
female    233
male      109
Name: Survived, dtype: int64

分组统计个数

survived_pclass = text['Survived'].groupby(text['Pclass']) 
---------------------------------------------
Pclass
1    136
2     87
3    119
Name: Survived, dtype: int64

联合分组

#以Pclass,Age分组,输出Fare的值
text.groupby(['Pclass','Age'])['Fare'].mean()
---------------------------------------------------------
Pclass  Age  
1       0.92     151.5500
        2.00     151.5500
        4.00      81.8583
        11.00    120.0000
        14.00    120.0000
                   ...   
3       61.00      6.2375
        63.00      9.5875
        65.00      7.7500
        70.50      7.7500
        74.00      7.7750
Name: Fare, Length: 182, dtype: float64

merge合并

a1= text['Fare'].groupby(text['Sex']).mean()
a2= text['Survived'].groupby(text['Sex']).sum()
result = pd.merge(a1,a2,on='Sex')
-----------------------------------------------
        Fare    Survived
Sex     
female  44.479818   233
male    25.523893   109

相关文章

网友评论

      本文标题:3、pandas数据重构

      本文链接:https://www.haomeiwen.com/subject/ocewjktx.html