第2次作业-titanic数据集练习
一、读入titanic.xlsx文件,按照教材示例步骤,完成数据清洗。
titanic数据集包含11个特征,分别是:
Survived:0代表死亡,1代表存活
Pclass:乘客所持票类,有三种值(1,2,3)
Name:乘客姓名
Sex:乘客性别
Age:乘客年龄(有缺失)
SibSp:乘客兄弟姐妹/配偶的个数(整数值)
Parch:乘客父母/孩子的个数(整数值)
Ticket:票号(字符串)
Fare:乘客所持票的价格(浮点数,0-500不等)
Cabin:乘客所在船舱(有缺失)
Embark:乘客登船港口:S、C、Q(有缺失)
1、
1
2
3
|
import pandas as pd titanic = pd.DataFrame(pd.read_excel( 'titanic-2.xlsx' )) titanic.head() |
2、
1
2
3
|
# 删除无效列 titanic.drop( 'embark_town' , axis = 1 , inplace = True ) titanic.head() |
3、
1
2
|
# 查找重复值 titanic.duplicated() |
4、
1
2
3
|
# 删除重复值 titanic = titanic.drop_duplicates() titanic.head() |
5、
1
2
|
# 统计空值的个数 titanic[ 'who' ].isnull().value_counts() |
6、
1
2
3
|
# 使用fillna方法填充空值 titanic[ 'who' ] = titanic[ 'who' ] .fillna( 'man' ) titanic |
7、
1
|
titanic[ 'age' ].isnull().value_counts() |
8、
1
2
3
|
# 使用fillna方法为age字段填充平均值 titanic[ 'age' ] = titanic[ 'age' ] .fillna(titanic[ 'age' ].mean()) titanic.head() |
9、
1
2
|
#使用describe查看统计信息 titanic.describe() |
10、
1
2
|
# 将异常值替换为平均值 titanic.replace([ 512.329200 ],titanic[ 'fare' ].mean()) |
二、对titanic数据集完成以下统计操作
1.统计乘客死亡和存活人数
1
|
titanic[ 'survived' ].value_counts() |
2.统计乘客中男女性别人数
1
|
titanic[ 'sex' ].value_counts() |
3.统计男女获救的人数
1
|
titanic[ 'sex' ][titanic[ 'survived' ] = = 1 ].value_counts() |
4.统计乘客所在的船舱等级的人数
1
|
titanic[ 'class' ].value_counts() |
5.使用corr()函数,判断两个属性是否具有相关性,分析舱位的高低和存活率的关系
1
|
titanic[ 'survived' ].corr(titanic[ 'pclass' ]) |
结果为负数,呈负相关,舱位越高,存活率越低。
6.画出乘客票价与舱位等级的箱体图Boxplot,从图中能够得到哪些结论?
1
|
titanic.boxplot([ 'fare' ],[ 'pclass' ],grid = False ) |
一等票价格浮动较大,二等、三等票价格浮动较小。