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

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

全部参考 datawhale-动手学数据分析

导入numpy、pandas包和数据

import numpy as np
import pandas as pd
print(pd.__version__)  # 1.0.3

载入数据

df=pd.read_csv('data/train-left-up.csv')
df.head()

1. 数据重构

1.1 数据的合并

载入数据:

#写入代码
train_left_up = pd.read_csv('data/train-left-up.csv')
train_left_down = pd.read_csv('data/train-left-down.csv')
train_right_up = pd.read_csv('data/train-right-up.csv')
train_right_down = pd.read_csv('data/train-right-down.csv')

1.2 pandas的连接函数concat()函数

使用concat方法:将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up

result_up=pd.concat([train_left_up,train_right_up],axis=1)
result_up.head()

使用concat方法:将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。然后将上边的result_up和result_down纵向合并为result

result_down = pd.concat([train_left_down,train_right_down],axis=1)
result = pd.concat([result_up,result_down])
result.head()

使用DataFrame自带的方法join方法和append:将数据train-left-up.csv和train-right-up.csv横向合并为一张result_up表,将train-left-down和train-right-down横向合并为一张result_down表,然后将上边的result_up和result_down纵向合并为result

resul_up = train_left_up.join(train_right_up)
result_down = train_left_down.join(train_right_down)
result = result_up.append(result_down)
result.head()

使用Panads的merge方法和DataFrame的append方法完成四个表的合并

result_up = pd.merge(train_left_up,train_right_up,left_index=True,right_index=True)
result_down = pd.merge(train_left_down,train_right_down,left_index=True,right_index=True)
result = resul_up.append(result_down)
result.head()

保存数据

result.to_csv('result.csv')

2. 将数据变为Series类型的数据

df = pd.read_csv('result.csv')
df1=df.stack().head(20)
df1.head()
# 0  Unnamed: 0                           0
#    PassengerId                          1
#    Survived                             0
#    Pclass                               3
#    Name           Braund, Mr. Owen Harris
# dtype: object

3. 数据运用

df = pd.read_csv('result.csv')
df.head()
  1. 计算泰坦2号男性与女性的平均票价
df1  = df['Fare'].groupby(df['Sex'])
df1.mean()
# Sex
# female    44.479818
# male      25.523893
# Name: Fare, dtype: float64
  1. 统计泰坦尼克号中男女的存活人数
survived_sex = df['Survived'].groupby(df['Sex']).sum()
survived_sex.head()

# Sex
# female    233
# male      109
# Name: Survived, dtype: int64
  1. 计算客舱不同等级的存活人数
survived_pclass = df['Survived'].groupby(df['Pclass']).sum()
survived_pclass
# Pclass
# 1    136
# 2     87
# 3    119
# Name: Survived, dtype: int64
  1. 统计在不同等级的票中的不同年龄的船票花费的平均值
df.groupby(['Pclass','Age'])['Fare'].mean().head()
# Pclass  Age  
# 1       0.92     151.5500
#         2.00     151.5500
#         4.00      81.8583
#         11.00    120.0000
#         14.00    120.0000
# Name: Fare, dtype: float64
  1. 将2和3的数据合并,并保存到sex_fare_survived.csv
result = pd.merge(df1.mean(),survived_sex,on='Sex')
result
  1. 得出不同年龄的总的存活人数,然后找出存活人数的最高的年龄,最后计算存活人数最高的存活率(存活人数/总人数)
survived_age = df['Survived'].groupby(df['Age']).sum()
survived_age.head()
# Age
# 0.42    1
# 0.67    1
# 0.75    2
# 0.83    2
# 0.92    1
# Name: Survived, dtype: int64
survived_age[survived_age.values==survived_age.max()]  
# Age
# 24.0    15
# Name: Survived, dtype: int64 
_sum = df['Survived'].sum()
precetn =survived_age.max()/_sum
print("最大存活率:"+str(precetn))
# 最大存活率:0.043859649122807015
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值