数据分析实战:泰坦尼克的任务——from kaggle(连载3)

这门课程得主要目的是通过真实的数据,以实战的方式了解数据分析的流程和熟悉数据分析python的基本操作。知道了课程的目的之后,我们接下来我们要正式的开始数据分析的实战教学,完成kaggle上泰坦尼克的任务,实战数据分析全流程。

代码都是jupyter形式

数据重构(上)

5.1数据的合并

import numpy as np
import pandas as pd

5.1.1 任务一:将data文件夹里面的所有数据都载入,与之前的原始 数据相比,观察他们的之间的关系

text_left_up = pd.read_csv(r"C:\Users\win 10\Desktop\train-left-up.csv")
text_left_down = pd.read_csv(r"C:\Users\win 10\Desktop\train-left-down.csv")
text_right_up = pd.read_csv(r"C:\Users\win 10\Desktop\train-right-up.csv")
text_right_down = pd.read_csv(r"C:\Users\win 10\Desktop\train-right-down.csv")
text_left_up.head()

在这里插入图片描述

text_left_down.head()

在这里插入图片描述

text_right_up.head()

在这里插入图片描述

text_right_down.head()

在这里插入图片描述

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

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

在这里插入图片描述

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

result_down = pd.concat([text_left_down,text_right_down],axis=1).head()
result_down

在这里插入图片描述

result = pd.concat([result_up,result_down])
result

在这里插入图片描述

5.1.4 任务四:使用DataFrame自带的方法join方法和append:完成 任务二和任务三的任务

result_up = text_left_up.join(text_right_up)
result_down = text_left_up.join(text_right_down)
result = result_up.append(result_down)
result.head()

在这里插入图片描述

5.1.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 = result_up.append(result_down)
result.head()

在这里插入图片描述

5.1.6 任务六:完成的数据保存为result.csv

result.to_csv(r"C:\Users\win 10\Desktop\result.csv")

5.2 换一种角度看数据

5.2.1 任务一:将数据变为Series类型的数据

text = pd.read_csv(r"C:\Users\win 10\Desktop\result.csv")
text.head()

在这里插入图片描述

unit_result=text.stack().head(20)
unit_result.head()

stack()函数:
在这里插入图片描述
表格在行列方向上均有索引(类似于DataFrame),花括号结构只有“列方向”上的索引(类似于层次化的Series),结构更加偏向于堆叠(Series-stack,方便记忆)。stack函数会将数据从”表格结构“变成”花括号结构“,即将其行索引变成列索引,
在这里插入图片描述

unit_result.to_csv(r'C:\Users\win 10\Desktop\unit_result.csv')
test = pd.read_csv(r'C:\Users\win 10\Desktop\unit_result.csv')
test.head()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值