数据重构

数据重构

先将数据载入,利用head()方法查看数据的信息

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

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

stack函数的作用

下面的链接有详细的解释
添加链接描述

计算数据

计算泰坦尼克号男性与女性的平均票价

df  = text['Fare'].groupby(text['Sex'])
means = df.mean()
means

统计泰坦尼克号中男女的存活人数

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

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

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

利用agg函数计算

df.groupby('Survived').agg({'Sex': 'mean', 'Pclass': 'count'}).rename(columns={'Sex': 'mean_sex', 'Pclass': 'count_pclass'})

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

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