一、读入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(有缺失)
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) titanic = pd.DataFrame(pd.read_excel('titanic.xlsx'))
titanic.head() print(titanic.head())
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.drop('embark_town',axis=1,inplace=True) titanic.head() print(titanic.head())
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.drop('embark_town',axis=1,inplace=True) titanic.head() titanic.duplicated() print(titanic.duplicated())
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.drop('embark_town',axis=1,inplace=True) titanic.head() titanic.drop_duplicates() titanic.head() print(titanic.head())
import pandas as pd
titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['age'].isnull().value_counts() print(titanic['age'].isnull().value_counts())
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['embarked'].isnull().value_counts() #查看embarked缺失 print(titanic['embarked'].isnull().value_counts())
import pandas as pd pd.set_option('display.max_columns', 1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth', 1000) titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.drop('embark_town',axis=1,inplace=True) titanic.head() titanic.drop_duplicates() titanic.head() titanic['age'] = titanic['age'].fillna(titanic['age'].mean()) #处理age缺失,取平均值 titanic['embarked'] = titanic['embarked'].fillna('S') #处理embarked缺失,取's' titanic.head() print(titanic.head())
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.describe()#异常值处理 print(titanic.describe())
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.drop('embark_town',axis=1,inplace=True) titanic.head() titanic.replace([512.329200], titanic['fare'].mean()) #修改fare异常值 print(titanic.replace([512.329200], titanic['fare'].mean()))
二、对titanic数据集完成以下统计操作
1.统计乘客死亡和存活人数
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['survived'].value_counts() print(titanic['survived'].value_counts())
2.统计乘客中男女性别人数
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['sex'].value_counts() #统计性别人数 print(titanic['sex'].value_counts())
3.统计男女获救的人数
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.groupby('survived')['sex'].value_counts().unstack() # 统计获救男女人数 print(titanic.groupby('survived')['sex'].value_counts().unstack())
4.统计乘客所在的船舱等级的人数
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['pclass'].value_counts() #统计乘客所在的船舱等级的人数 print(titanic['pclass'].value_counts())
5.使用corr()函数,判断两个属性是否具有相关性,分析舱位的高低和存活率的关系
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic['survived'].corr(titanic['pclass']) #分析舱位的高低和存活率的关系 print(titanic['survived'].corr(titanic['pclass']))
舱位越高,存活率越低。
6.画出乘客票价与舱位等级的箱体图Boxplot,从图中能够得到哪些结论?
import pandas as pd titanic = pd.DataFrame(pd.read_excel('titanic.xlsx')) titanic.head() titanic.boxplot(['fare'],['pclass']) print(titanic.boxplot(['fare'],['pclass']))