动手学数据分析3-数据重构

数据重构

一、数据合并

我们将之前的train.csv分成了四部分

前左(left-up)前右(right-up)
后左(left-down)后右 (right-down)
text_left_up = pd.read_csv("data/train-left-up.csv")
text_left_down = pd.read_csv("data/train-left-down.csv")
text_right_up = pd.read_csv("data/train-right-up.csv")
text_right_down = pd.read_csv("data/train-right-down.csv")
print(f"Left Up Rows: {len(text_left_up)}")
print(f"Right Up Rows: {len(text_right_up)}")
print(f"Left Down Rows: {len(text_left_down)}")
print(f"Right Down Rows: {len(text_right_down)}")
Left Up Rows: 439
Right Up Rows: 439
Left Down Rows: 452
Right Down Rows: 452
1.观察数据

train-left-up.csv前左(left-up)
train-right-up.csv在这里插入图片描述
train-left-down.csv后左(left-down)
train-right-down.csv后右 (right-down)

2.使用使用Panads的concat方法横向合并
# 横向合并left_up,right_up
list_up = [text_left_up,text_right_up]
result_up = pd.concat(list_up,axis=1)
result_up.head()

在这里插入图片描述

# 横向合并left_down,right_down
list_down=[text_left_down,text_right_down]
result_down = pd.concat(list_down,axis=1)
result_down.head()

在这里插入图片描述

3.同样使用Panads的concat方法纵向合并上面得到的result_up,result_down
# 纵向合并数据result_up, result_down
result = pd.concat([result_up,result_down])
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891

在这里插入图片描述

4.使用DataFrame的join方法和append方法也能实现前面的操作
# 横向合并left_up,right_up
resul_up = text_left_up.join(text_right_up)
# 横向合并left_down,right_down
result_down = text_left_down.join(text_right_down)
# 纵向合并数据result_up, result_down
result = result_up.append(result_down)
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891

在这里插入图片描述

5.使用Panads的merge方法和DataFrame的append方法也能实现上述合并操作
result_up = pd.merge(text_left_up,text_right_up,left_index=True,right_index=True)
result_down = pd.merge(text_left_down,text_right_down,left_index=True,right_index=True)
result = resul_up.append(result_down)
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891

在这里插入图片描述

二、数据运用

在这里我们使用了一些聚合函数来分析数据

  1. 计算泰坦尼克号男性与女性的平均票价
mean_fare = result['Fare'].groupby(result['Sex']).mean()
Sex
female    44.479818
male      25.523893
Name: Fare, dtype: float64
  1. 统计泰坦尼克号中男女的存活人数 (Survived列中活着记为1,死亡记为0)
survived_sex = result['Survived'].groupby(result['Sex']).sum()
Sex
female    233
male      109
Name: Survived, dtype: int64
  1. 计算合并前面计算的值
pd.merge(means,survived_sex,on='Sex')

在这里插入图片描述

  1. 计算客舱不同等级的存活人数
# 计算客舱不同等级的存活人数
survived_pclass = result['Survived'].groupby(result['Pclass']).sum()

Pclass
1    136
2     87
3    119
Name: Survived, dtype: int64
  1. 以上运算可以通过agg()函数来同时计算,并且可以使用rename函数修改列名。
result.groupby('Sex').agg({'Sex':'count','Age':'mean','Fare': 'mean','Survived':'sum'}).rename(columns=
                            {'Sex':'人数','Age':'平均年龄', 'Fare': '平均票价','Survived':'存活人数'})

在这里插入图片描述

  1. 统计在不同等级的票中的不同性别的船票花费的平均值
result.groupby(['Pclass','Sex'])['Fare'].mean()
Pclass  Sex   
1       female    106.125798
        male       67.226127
2       female     21.970121
        male       19.741782
3       female     16.118810
        male       12.661633
Name: Fare, dtype: float64
  1. 得出不同年龄的总的存活人数,然后找出存活人数的最多的年龄,最后计算存活人数最多的年龄人数占总存活人数的比率(存活人数最多的年龄人数/存活总人数)
# 不同年龄的存活人数
survived_age = result['Survived'].groupby(result['Age']).sum()
# 找出最大值的年龄段
survived_age_max = survived_age[survived_age.values==survived_age.max()]
# 计算存活总人数
survived_sum = result['Survived'].sum()
print(f"存活总人数:{survived_sum}")
# 计算存活比率
survived_precent =survived_age.max()/survived_sum
print(f"年龄在{survived_age_max.index[0]}岁的存活最多,人数为{survived_age_max.values[0]}人,占所有存活人数的比率为:{survived_precent}")
存活总人数:342
年龄在24.0岁的存活最多,人数为15人,占所有存活人数的比率为:0.043859649122807015
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值