数据分析之数据重构

第二章:数据重构

2.4:数据的合并

2.4.1:将data文件夹里面的所有数据都载入,与之前的原始数据相比,观察他们的之间的关系
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")

# 展示数据
text_left_up.head()
text_left_down.head()
text_right_down.head()
text_right_up.head()
text_left_up的乘客在text_left_down的前面
2.4.2:使用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)
result_up.head()
2.4.3 :使用concat方法:将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。然后将上边的result_up和result_down纵向合并为result。
list_down=[text_left_down,text_right_down]
result_down = pd.concat(list_down,axis=1)
result = pd.concat([result_up,result_down])
result.head()
无axit参数,默认为纵向合并,axit=1为横向合并
2.4.4 :使用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)
result.head()
join表示横向合并,append表示纵向合并
2.4.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)
result.head()
merge为横向合并列(并且需要有共同列拼接),类似于join,在这使用行索引来拼接
【思考】对比merge、join以及concat的方法的不同以及相同。思考一下在任务四和任务五的情况下,为什么都要求使用DataFrame的append方法,如何只要求使用merge或者join可不可以完成任务四和任务五呢?

df.merge(): 可以根据一个或多个键将不同DataFrame中的行连接起来,类似于数据库中的join方法。
join 连接主要是列索引上的合并,join默认为左连接,只能操作DataFrame,Series没有该方法。
append 相当与concat的简易操作, 行上的拼接(axis=0),需要列的名称相同。
concat是轴向链接,关键在与axis,axis=0表示列操作,axis=1 行操作,默认为0
2.4.6 :完成的数据保存为result.csv
result.to_csv('result.csv')

2.5 换一种角度看数据

2.5.1 :将我们的数据变为Series类型的数据
# 将完整的数据加载出来
text = pd.read_csv('result.csv')
text.head()

# 代码写在这里
unit_result=text.stack().head(20)
unit_result.head()

#将代码保存为unit_result,csv
unit_result.to_csv('unit_result.csv')
test = pd.read_csv('unit_result.csv')
test.head()
stack函数将原数据的行变成列,展示出每个元素的所有数据

2.6 数据运用

2.6.1:通过《Python for Data Analysis》P303、Google or Baidu来学习了解GroupBy机制

先分类后合并

#将数据集按照company字段进行划分
group = data.groupby("company")
#如果直接输出group,会得到一个DataFrameGroupBy对象
#需要转化成list来看
list(group)
先分类后聚合

GroupBy

2.6.2:计算泰坦尼克号男性与女性的平均票价
df  = text['Fare'].groupby(text['Sex'])
means = df.mean()
means
2.6.3:统计泰坦尼克号中男女的存活人数
survived_sex = text['Survived'].groupby(text['Sex']).sum()
survived_sex.head()
2.6.4:计算客舱不同等级的存活人数
survived_pclass = text['Survived'].groupby(text['Pclass'])
survived_pclass.sum()
#【思考】从任务二到任务三中,这些运算可以通过agg()函数来同时计算。并且可以使用rename函数修改列名。你可以按照提示写出这个过程吗?
text.groupby('Sex').agg({'Fare': 'mean', 'Pclass': 'count'}).rename(columns=
                            {'Fare': 'mean_fare', 'Pclass': 'count_pclass'})
2.6.5:统计在不同等级的票中的不同年龄的船票花费的平均值
text.groupby(['Pclass','Age'])['Fare'].mean().head()
2.6.6:将任务二和任务三的数据合并,并保存到sex_fare_survived.csv
result = pd.merge(means,survived_sex,on='Sex')
result
result.to_csv('sex_fare_survived.csv')
2.6.7:得出不同年龄的总的存活人数,然后找出存活人数最多的年龄段,最后计算存活人数最高的存活率(存活人数/总人数)
#不同年龄的存活人数
survived_age = text['Survived'].groupby(text['Age']).sum()
survived_age.head()

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

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

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

precetn =survived_age.max()/_sum

print("最大存活率:"+str(precetn))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值