ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

动手写数据分析:2(中)数据重构

2021-12-17 22:59:55  阅读:133  来源: 互联网

标签:数据分析 重构 right text up down 动手 result csv


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()

 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()

2.4.4  使用DataFrame自带的方法join方法和append:完成2.4.2和2.4.3的任务

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()

 2.4.5 使用Panads的merge方法和DataFrame的append方法:完成2.4.2和2.4.3的任务

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()

2.4.6 完成的数据保存为result.csv

result.to_csv('result.csv')

 

2.5 换一种角度看数据

2.5.1 :将我们的数据变为Series类型的数据


这个stack函数是实现输入数个数组不同方式的堆叠,返回堆叠后的1个数组。

# 将完整的数据加载出来
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()

 2.6 数据运用

2.6.1  了解GroupBy机制

       GroupBy是Pandas提供的强大的数据聚合处理机制,可以对大量级的多维数据进行透视,同时GroupBy还提供强大的apply函数,使得在多维数据中应用复杂函数得到复杂结果成为可能(这也是个人认为在实际业务分析中,数据量没那么大的情况下,Pandas相较于Excel透视表最有优势的一点).从抽象的“道”层面,在不涉及具体代码的情况下,我们去理解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()

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)


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

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

precetn =survived_age.max()/_sum

print("最大存活率:"+str(precetn))

 

标签:数据分析,重构,right,text,up,down,动手,result,csv
来源: https://blog.csdn.net/jassnsnn/article/details/122005853

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有