Chapter 04
2 第二章:数据重构
NotebookPython 329 cells
2 第二章:数据重构
In [14]python · cell 2
python
# 导入基本库
import numpy as np
import pandas as pdIn [15]python · cell 3
python
# 载入data文件中的:result.csv
text = pd.read_csv('result.csv')
text.head()Output
Unnamed: 0 PassengerId Survived Pclass \
0 0 1 0 3
1 1 2 1 1
2 2 3 1 3
3 3 4 1 1
4 4 5 0 3
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1.0
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1.0
2 Heikkinen, Miss. Laina female 26.0 0.0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1.0
4 Allen, Mr. William Henry male 35.0 0.0
Parch Ticket Fare Cabin Embarked
0 0.0 A/5 21171 7.2500 NaN S
1 0.0 PC 17599 71.2833 C85 C
2 0.0 STON/O2. 3101282 7.9250 NaN S
3 0.0 113803 53.1000 C123 S
4 0.0 373450 8.0500 NaN S
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| Unnamed: 0 | PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1.0 | 0.0 | A/5 21171 | 7.2500 | NaN | S |
| 1 | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1.0 | 0.0 | PC 17599 | 71.2833 | C85 | C |
| 2 | 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0.0 | 0.0 | STON/O2. 3101282 | 7.9250 | NaN | S |
| 3 | 3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1.0 | 0.0 | 113803 | 53.1000 | C123 | S |
| 4 | 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0.0 | 0.0 | 373450 | 8.0500 | NaN | S |
第一部分:数据聚合与运算
2.6 数据运用
2.6.1 任务一:通过《Python for Data Analysis》P303、Google or Baidu来学习了解GroupBy机制
In [3]python · cell 7
python
#写入心得在了解GroupBy机制之后,运用这个机制完成一系列的操作,来达到我们的目的。
下面通过几个任务来熟悉GroupBy机制。
2.4.2:任务二:计算泰坦尼克号男性与女性的平均票价
In [4]python · cell 10
python
df = text['Fare'].groupby(text['Sex'])
means = df.mean()
meansOutput
Sex female 44.479818 male 25.523893 Name: Fare, dtype: float64
2.4.3:任务三:统计泰坦尼克号中男女的存活人数
In [5]python · cell 12
python
survived_sex = text['Survived'].groupby(text['Sex']).sum()
survived_sex.head()Output
Sex female 233 male 109 Name: Survived, dtype: int64
2.4.4:任务四:计算客舱不同等级的存活人数
In [6]python · cell 14
python
survived_pclass = text['Survived'].groupby(text['Pclass'])
survived_pclass.sum()Output
Pclass 1 136 2 87 3 119 Name: Survived, dtype: int64
【提示:】表中的存活那一栏,可以发现如果还活着记为1,死亡记为0
【思考:】从数据分析的角度,上面的统计结果可以得出那些结论
In [7]python · cell 17
python
#思考心得
【思考】从任务二到任务三中,这些运算可以通过agg()函数来同时计算。并且可以使用rename函数修改列名。你可以按照提示写出这个过程吗?
In [16]python · cell 19
python
#例子:
text.groupby('Sex').agg({'Fare': 'mean', 'Pclass': 'count'}).rename(columns=
{'Fare': 'mean_fare', 'Pclass': 'count_pclass'})
Output
mean_fare count_pclass Sex female 44.479818 314 male 25.523893 577
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| mean_fare | count_pclass | |
|---|---|---|
| Sex | ||
| female | 44.479818 | 314 |
| male | 25.523893 | 577 |
2.4.5:任务五:统计在不同等级的票中的不同年龄的船票花费的平均值
In [8]python · cell 21
python
text.groupby(['Pclass','Age'])['Fare'].mean().head()Output
Pclass Age
1 0.92 151.5500
2.00 151.5500
4.00 81.8583
11.00 120.0000
14.00 120.0000
Name: Fare, dtype: float642.4.6:任务六:将任务二和任务三的数据合并,并保存到sex_fare_survived.csv
In [9]python · cell 23
python
result = pd.merge(means,survived_sex,on='Sex')
resultOutput
Fare Survived Sex female 44.479818 233 male 25.523893 109
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
| Fare | Survived | |
|---|---|---|
| Sex | ||
| female | 44.479818 | 233 |
| male | 25.523893 | 109 |
In [10]python · cell 24
python
result.to_csv('sex_fare_survived.csv')2.4.7:任务七:得出不同年龄的总的存活人数,然后找出存活人数最多的年龄段,最后计算存活人数最高的存活率(存活人数/总人数)
In [11]python · cell 26
python
#不同年龄的存活人数
survived_age = text['Survived'].groupby(text['Age']).sum()
survived_age.head()Output
Age 0.42 1 0.67 1 0.75 2 0.83 2 0.92 1 Name: Survived, dtype: int64
In [12]python · cell 27
python
#找出最大值的年龄段
survived_age[survived_age.values==survived_age.max()]Output
Age 24.0 15 Name: Survived, dtype: int64
In [13]python · cell 28
python
_sum = text['Survived'].sum()
print(_sum)Output
342
In [14]python · cell 29
python
#首先计算总人数
_sum = text['Survived'].sum()
print("sum of person:"+str(_sum))
precetn =survived_age.max()/_sum
print("最大存活率:"+str(precetn))Output
sum of person:342 最大存活率:0.043859649122807015
