数据重构
一、数据合并
我们将之前的train.csv分成了四部分
前左(left-up) | 前右(right-up) |
后左(left-down) | 后右 (right-down) |
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")
print(f"Left Up Rows: {len(text_left_up)}")
print(f"Right Up Rows: {len(text_right_up)}")
print(f"Left Down Rows: {len(text_left_down)}")
print(f"Right Down Rows: {len(text_right_down)}")
Left Up Rows: 439
Right Up Rows: 439
Left Down Rows: 452
Right Down Rows: 452
1.观察数据
train-left-up.csv
train-right-up.csv
train-left-down.csv
train-right-down.csv
2.使用使用Panads的concat方法横向合并
# 横向合并left_up,right_up
list_up = [text_left_up,text_right_up]
result_up = pd.concat(list_up,axis=1)
result_up.head()
# 横向合并left_down,right_down
list_down=[text_left_down,text_right_down]
result_down = pd.concat(list_down,axis=1)
result_down.head()
3.同样使用Panads的concat方法纵向合并上面得到的result_up,result_down
# 纵向合并数据result_up, result_down
result = pd.concat([result_up,result_down])
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891
4.使用DataFrame的join方法和append方法也能实现前面的操作
# 横向合并left_up,right_up
resul_up = text_left_up.join(text_right_up)
# 横向合并left_down,right_down
result_down = text_left_down.join(text_right_down)
# 纵向合并数据result_up, result_down
result = result_up.append(result_down)
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891
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)
print(f"Result Rows:{len(result)}")
result.head()
Result Rows:891
二、数据运用
在这里我们使用了一些聚合函数来分析数据
- 计算泰坦尼克号男性与女性的平均票价
mean_fare = result['Fare'].groupby(result['Sex']).mean()
Sex
female 44.479818
male 25.523893
Name: Fare, dtype: float64
- 统计泰坦尼克号中男女的存活人数 (Survived列中活着记为1,死亡记为0)
survived_sex = result['Survived'].groupby(result['Sex']).sum()
Sex
female 233
male 109
Name: Survived, dtype: int64
- 计算合并前面计算的值
pd.merge(means,survived_sex,on='Sex')
- 计算客舱不同等级的存活人数
# 计算客舱不同等级的存活人数
survived_pclass = result['Survived'].groupby(result['Pclass']).sum()
Pclass
1 136
2 87
3 119
Name: Survived, dtype: int64
- 以上运算可以通过agg()函数来同时计算,并且可以使用rename函数修改列名。
result.groupby('Sex').agg({'Sex':'count','Age':'mean','Fare': 'mean','Survived':'sum'}).rename(columns=
{'Sex':'人数','Age':'平均年龄', 'Fare': '平均票价','Survived':'存活人数'})
- 统计在不同等级的票中的不同性别的船票花费的平均值
result.groupby(['Pclass','Sex'])['Fare'].mean()
Pclass Sex
1 female 106.125798
male 67.226127
2 female 21.970121
male 19.741782
3 female 16.118810
male 12.661633
Name: Fare, dtype: float64
- 得出不同年龄的总的存活人数,然后找出存活人数的最多的年龄,最后计算存活人数最多的年龄人数占总存活人数的比率(存活人数最多的年龄人数/存活总人数)
# 不同年龄的存活人数
survived_age = result['Survived'].groupby(result['Age']).sum()
# 找出最大值的年龄段
survived_age_max = survived_age[survived_age.values==survived_age.max()]
# 计算存活总人数
survived_sum = result['Survived'].sum()
print(f"存活总人数:{survived_sum}")
# 计算存活比率
survived_precent =survived_age.max()/survived_sum
print(f"年龄在{survived_age_max.index[0]}岁的存活最多,人数为{survived_age_max.values[0]}人,占所有存活人数的比率为:{survived_precent}")
存活总人数:342
年龄在24.0岁的存活最多,人数为15人,占所有存活人数的比率为:0.043859649122807015