第二章 数据的重构
2.4.1 任务一:将data文件夹里面的所有数据都载入,与之前的原始数据相比,观察他们的之间的关系
经观察发现,读取的这些数据分别是train.csv的一部分
2.4.2:任务二:使用concat方法:将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up
百度查询concat的使用方法
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:完成任务二和任务三的任务
join的使用方法
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()
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()
另存为result.csv
result.to_csv('result.csv')
刷新右边数据出现result.csv
merge是连接,类似sql中的join,有内连,左连,右连和全连接几种。join是根据相同index进行连接,对于名称相同的属性,会保留。concat是两个数据集直接合并,可以在列上合并,也可在行上合并
2.5 换一种角度看数据
2.4.2:任务二:计算泰坦尼克号男性与女性的平均票价
任务三:统计泰坦尼克号中男女的存活人数
任务四:计算客舱不同等级的存活人数
任务五:统计在不同等级的票中的不同年龄的船票花费的平均值
任务六:将任务二和任务三的数据合并,并保存到sex_fare_survived.csv
任务七:得出不同年龄的总的存活人数,然后找出存活人数的最高的年龄,最后计算存活人数最高的存活率(存活人数/总人数)
survived_age = text['Survived'].groupby(text['Age']).sum()
survived_age.head()
找出最大值的年龄段
survived_age[survived_age.values==survived_age.max()]