4_数据分析—数据重构

一、数据的合并

1.1 导入基本库

# 导入基本库
import numpy as np
import pandas as pd

1.2 载入数据

#将data文件夹里面的所有数据都载入,与之前的原始数据相比,观察他们的之间的关系
text_left_up = pd.read_csv("train-left-up.csv")
text_left_down = pd.read_csv("train-left-down.csv")
text_right_up = pd.read_csv("train-right-up.csv")
text_right_down = pd.read_csv("train-right-down.csv")
text = pd.read_csv('train.csv')
 
#四个数据就是整体的数据被按照左上、左下、右上、右下分成四块。

image-20220205013050824

1.3 数据合并

1.3.1 方法一:concat方法

# 使用concat方法:将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up
list_up = [text_left_up,text_right_up]
result_up = pd.concat(list_up,axis=1)


# 使用concat方法:将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。
list_down=[text_left_down,text_right_down]
result_down = pd.concat(list_down,axis=1)

# 然后将上边的result_up和result_down纵向合并为result。
result = pd.concat([result_up,result_down])

image-20220205014055801

1.3.2 方法二:join和append方法

# 使用DataFrame自带的方法join方法和append

resul_up = text_left_up.join(text_right_up)

result_down = text_left_down.join(text_right_down)

result = result_up.append(result_down)

1.3.3 方法三:merge方法和append方法

# 使用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)

使用append才可以进行纵向的拼接(可追加行)。

只有merge,join不行,因为两者都是横向拼接。

二、换一种角度看数据

2.1 将DataFrame类型数据变为Series类型数据

#这个stack函数是干什么的?
#将数据从表格结构变成花括号结构,即将其行索引变成列索引
unit_result=text.stack().head(20)

image-20220205015329571

#将代码保存为unit_result,csv
unit_result.to_csv('unit_result.csv')

test = pd.read_csv('unit_result.csv')

test.head()

image-20220205015532582

三、数据聚合与运算(泰坦尼克号数据集)

数据重构依旧属于数据理解(准备)的范围

3.1 groupby() 用法

根据DataFrame本身的某一列或多列内容进行分组聚合

3.1.1 计算男性与女性的平均票价

# 将上面的数据集按照Sex字段进行划分
df  = text['Fare'].groupby(text['Sex'])
means = df.mean()
means

image-20220205203445932

3.1.2 统计男女的存活人数

survived_sex = text['Survived'].groupby(text['Sex']).sum()
survived_sex.head()

image-20220205203541275

3.1.3 计算客舱不同等级的存活人数

survived_pclass = text['Survived'].groupby(text['Pclass'])
survived_pclass.sum()

image-20220205203640842

表中的存活那一栏,可以发现如果还活着记为1,死亡记为0

3.1.4 统计在不同等级的票中的不同年龄的船票花费的平均值

text.groupby(['Pclass','Age'])['Fare'].mean().head()

image-20220205213440190

3.1.5 得出不同年龄的总的存活人数,然后找出存活人数最多的年龄段,最后计算存活人数最高的存活率(存活人数/总人数)

#不同年龄的存活人数
survived_age = text['Survived'].groupby(text['Age']).sum()
survived_age.head()

image-20220205214304030

#找出最大值的年龄段
survived_age[survived_age.values==survived_age.max()]

image-20220205214352888

#首先计算总人数
_sum = text['Survived'].sum()

print("sum of person:"+str(_sum))

precetn =survived_age.max()/_sum

print("最大存活率:"+str(precetn))
sum of person:342
最大存活率:0.043859649122807015

3.2 agg()函数用法

3.1.1和3.1.2可以用agg()函数来同时计算。并且可以使用rename函数修改列名。

聚合函数,对分组后数据进行聚合,默认情况对分组后其他列进行聚合。

text.groupby('Sex').agg({'Fare': 'mean', 'Pclass': 'count'}).rename(columns=
                            {'Fare': 'mean_fare', 'Pclass': 'count_pclass'})

image-20220205205522795

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

少云清

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值