前言:datawhale团队贡献的课程——《动手学数据分析》,这门课程得主要目的是通过真实的数据,以实战的方式了解数据分析的流程和熟悉数据分析python的基本操作。完成kaggle上泰坦尼克的任务,实战数据分析全流程。 资料:教材《Python for Data Analysis》
1 第一章:数据载入及初步观察
1.1 载入数据
数据集下载 https://www.kaggle.com/c/titanic/overview
1.1.1 任务一:导入numpy和pandas
import numpy as np
import pandas as pd
【提示】如果加载失败,学会如何在你的python环境下安装numpy和pandas这两个库 pip install numpy pip install pandas
1.1.2 任务二:载入数据
(1) 使用相对路径载入数据 (2) 使用绝对路径载入数据
train = pd.read_csv("train.csv")
train.head()
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
train = pd.read_csv("C:/Users/JJY/Desktop/hands-on-data-analysis-master/第一单元项目集合/train.csv",engine='python')
train.head()
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
【提示】相对路径载入报错时,尝试使用os.getcwd()查看当前工作目录。 【思考】知道数据加载的方法后,试试pd.read_csv()和pd.read_table()的不同,如果想让他们效果一样,需要怎么做?了解一下’.tsv’和’.csv’的不同,如何加载这两个数据集? 【总结】加载的数据是所有工作的第一步,我们的工作会接触到不同的数据格式(eg:.csv;.tsv;.xlsx),但是加载的方法和思路都是一样的,在以后工作和做项目的过程中,遇到之前没有碰到的问题,要多多查资料吗,使用googel,了解业务逻辑,明白输入和输出是什么。
train = pd.read_table("train.csv",sep=",")
train.head()
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
1.1.3 任务三:每1000行为一个数据模块,逐块读取
chunker = pd.read_csv("train.csv",chunksize=1000)
print(type(chunker))
<class 'pandas.io.parsers.TextFileReader'>
【思考】什么是逐块读取?为什么要逐块读取呢? –按指定大小分块读取大数据,避免因数据量大导致内存不足,但也会更耗时一些,数据的处理和清洗经常使用分块的方式处理
1.1.4 任务四:将表头改成中文,索引改为乘客ID [对于某些英文资料,我们可以通过翻译来更直观的熟悉我们的数据]
PassengerId => 乘客ID Survived => 是否幸存 Pclass => 乘客等级(1/2/3等舱位) Name => 乘客姓名 Sex => 性别 Age => 年龄 SibSp => 堂兄弟/妹个数 Parch => 父母与小孩个数 Ticket => 船票信息 Fare => 票价 Cabin => 客舱 Embarked => 登船港口
train = pd.read_csv("train.csv", names=["乘客ID","是否幸存","乘客等级","姓名","性别","年龄","堂兄弟/妹个数","父母子女个数","船票信息","票价","客舱","登船港口"],index_col="乘客ID",header=0)
train.head()
| 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
乘客ID | | | | | | | | | | | |
---|
1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
1.2 初步观察
导入数据后,你可能要对数据的整体结构和样例进行概览,比如说,数据大小、有多少列,各列都是什么格式的,是否包含null等
1.2.1 任务一:查看数据的基本信息
train.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 891 entries, 1 to 891
Data columns (total 11 columns):
是否幸存 891 non-null int64
乘客等级 891 non-null int64
姓名 891 non-null object
性别 891 non-null object
年龄 714 non-null float64
堂兄弟/妹个数 891 non-null int64
父母子女个数 891 non-null int64
船票信息 891 non-null object
票价 891 non-null float64
客舱 204 non-null object
登船港口 889 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 83.5+ KB
#【提示】有多个函数可以这样做,你可以做一下总结
train.isnull().sum(axis=0)
是否幸存 0
乘客等级 0
姓名 0
性别 0
年龄 177
堂兄弟/妹个数 0
父母子女个数 0
船票信息 0
票价 0
客舱 687
登船港口 2
dtype: int64
1.2.2 任务二:观察表格前10行的数据和后15行的数据
train.head(10)
| 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
乘客ID | | | | | | | | | | | |
---|
1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
6 | 0 | 3 | Moran, Mr. James | male | NaN | 0 | 0 | 330877 | 8.4583 | NaN | Q |
---|
7 | 0 | 1 | McCarthy, Mr. Timothy J | male | 54.0 | 0 | 0 | 17463 | 51.8625 | E46 | S |
---|
8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2.0 | 3 | 1 | 349909 | 21.0750 | NaN | S |
---|
9 | 1 | 3 | Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) | female | 27.0 | 0 | 2 | 347742 | 11.1333 | NaN | S |
---|
10 | 1 | 2 | Nasser, Mrs. Nicholas (Adele Achem) | female | 14.0 | 1 | 0 | 237736 | 30.0708 | NaN | C |
---|
train.tail(15)
| 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
乘客ID | | | | | | | | | | | |
---|
877 | 0 | 3 | Gustafsson, Mr. Alfred Ossian | male | 20.0 | 0 | 0 | 7534 | 9.8458 | NaN | S |
---|
878 | 0 | 3 | Petroff, Mr. Nedelio | male | 19.0 | 0 | 0 | 349212 | 7.8958 | NaN | S |
---|
879 | 0 | 3 | Laleff, Mr. Kristo | male | NaN | 0 | 0 | 349217 | 7.8958 | NaN | S |
---|
880 | 1 | 1 | Potter, Mrs. Thomas Jr (Lily Alexenia Wilson) | female | 56.0 | 0 | 1 | 11767 | 83.1583 | C50 | C |
---|
881 | 1 | 2 | Shelley, Mrs. William (Imanita Parrish Hall) | female | 25.0 | 0 | 1 | 230433 | 26.0000 | NaN | S |
---|
882 | 0 | 3 | Markun, Mr. Johann | male | 33.0 | 0 | 0 | 349257 | 7.8958 | NaN | S |
---|
883 | 0 | 3 | Dahlberg, Miss. Gerda Ulrika | female | 22.0 | 0 | 0 | 7552 | 10.5167 | NaN | S |
---|
884 | 0 | 2 | Banfield, Mr. Frederick James | male | 28.0 | 0 | 0 | C.A./SOTON 34068 | 10.5000 | NaN | S |
---|
885 | 0 | 3 | Sutehall, Mr. Henry Jr | male | 25.0 | 0 | 0 | SOTON/OQ 392076 | 7.0500 | NaN | S |
---|
886 | 0 | 3 | Rice, Mrs. William (Margaret Norton) | female | 39.0 | 0 | 5 | 382652 | 29.1250 | NaN | Q |
---|
887 | 0 | 2 | Montvila, Rev. Juozas | male | 27.0 | 0 | 0 | 211536 | 13.0000 | NaN | S |
---|
888 | 1 | 1 | Graham, Miss. Margaret Edith | female | 19.0 | 0 | 0 | 112053 | 30.0000 | B42 | S |
---|
889 | 0 | 3 | Johnston, Miss. Catherine Helen "Carrie" | female | NaN | 1 | 2 | W./C. 6607 | 23.4500 | NaN | S |
---|
890 | 1 | 1 | Behr, Mr. Karl Howell | male | 26.0 | 0 | 0 | 111369 | 30.0000 | C148 | C |
---|
891 | 0 | 3 | Dooley, Mr. Patrick | male | 32.0 | 0 | 0 | 370376 | 7.7500 | NaN | Q |
---|
1.2.4 任务三:判断数据是否为空,为空的地方返回True,其余地方返回False
train.isnull().head()
| 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
乘客ID | | | | | | | | | | | |
---|
1 | False | False | False | False | False | False | False | False | False | True | False |
---|
2 | False | False | False | False | False | False | False | False | False | False | False |
---|
3 | False | False | False | False | False | False | False | False | False | True | False |
---|
4 | False | False | False | False | False | False | False | False | False | False | False |
---|
5 | False | False | False | False | False | False | False | False | False | True | False |
---|
【总结】上面的操作都是数据分析中对于数据本身的观察
1.3 保存数据
1.3.1 任务一:将你加载并做出改变的数据,在工作目录下保存为一个新文件train_chinese.csv
train.to_csv("train_chinese.csv")
【总结】数据的加载以及入门,接下来就要接触数据本身的运算,我们将主要掌握numpy和pandas在工作和项目场景的运用。
1.4 知道你的数据叫什么
我们学习pandas的基础操作,那么上一节通过pandas加载之后的数据,其数据类型是什么呢?
开始前导入numpy和pandas
import numpy as np
import pandas as pd
1.4.1 任务一:pandas中有两个数据类型DateFrame和Series,通过查找简单了解他们。然后自己写一个关于这两个数据类型的小例子🌰[开放题]
data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
'year': [2000, 2001, 2002, 2001, 2002, 2003],'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
example_2 = pd.DataFrame(data)
print(example_2,type(example_2))
sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
example_1 = pd.Series(sdata)
print(example_1,type(example_1))
state year pop
0 Ohio 2000 1.5
1 Ohio 2001 1.7
2 Ohio 2002 3.6
3 Nevada 2001 2.4
4 Nevada 2002 2.9
5 Nevada 2003 3.2 <class 'pandas.core.frame.DataFrame'>
Ohio 35000
Texas 71000
Oregon 16000
Utah 5000
dtype: int64 <class 'pandas.core.series.Series'>
1.4.2 任务二:根据上节课的方法载入"train.csv"文件
train = pd.read_csv("train.csv")
也可以加载上一节课保存的"train_chinese.csv"文件。通过翻译版train_chinese.csv熟悉了这个数据集,然后我们对trian.csv来进行操作
1.4.3 任务三:查看DataFrame数据的每列的名称
train.columns
Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
dtype='object')
1.4.4任务四:查看"Cabin"这列的所有值[有多种方法]
train["Cabin"].head()
0 NaN
1 C85
2 NaN
3 C123
4 NaN
Name: Cabin, dtype: object
train.Cabin.head()
0 NaN
1 C85
2 NaN
3 C123
4 NaN
Name: Cabin, dtype: object
1.4.5 任务五:加载文件"test_1.csv",然后对比"train.csv",看看有哪些多出的列,然后将多出的列删除
经过我们的观察发现一个测试集test_1.csv有一列是多余的,我们需要将这个多余的列删去
test_1 = pd.read_csv("test_1.csv")
test_1.head()
| Unnamed: 0 | PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked | a |
---|
0 | 0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S | 100 |
---|
1 | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | 100 |
---|
2 | 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S | 100 |
---|
3 | 3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S | 100 |
---|
4 | 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S | 100 |
---|
del test_1["a"]
test_1.head()
| 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 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.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 | 113803 | 53.1000 | C123 | S |
---|
4 | 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
【思考】还有其他的删除多余的列的方式吗?
test_2 = test_1.drop("a",axis=1)
test_2.head()
| 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 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.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 | 113803 | 53.1000 | C123 | S |
---|
4 | 4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
1.4.6 任务六: 将[‘PassengerId’,‘Name’,‘Age’,‘Ticket’]这几个列元素隐藏,只观察其他几个列元素
train_1 = train.drop(['PassengerId','Name','Age','Ticket'],axis=1)
train_1.head()
| Survived | Pclass | Sex | SibSp | Parch | Fare | Cabin | Embarked |
---|
0 | 0 | 3 | male | 1 | 0 | 7.2500 | NaN | S |
---|
1 | 1 | 1 | female | 1 | 0 | 71.2833 | C85 | C |
---|
2 | 1 | 3 | female | 0 | 0 | 7.9250 | NaN | S |
---|
3 | 1 | 1 | female | 1 | 0 | 53.1000 | C123 | S |
---|
4 | 0 | 3 | male | 0 | 0 | 8.0500 | NaN | S |
---|
【思考】对比任务五和任务六,是不是使用了不一样的方法(函数),如果使用一样的函数如何完成上面的不同的要求呢? –使用del, 一次只能删除一列,不能一次删除多列,使用drop方法一次删除多列
【思考回答】
如果想要完全的删除你的数据结构,使用inplace=True,因为使用inplace就将原数据覆盖了,所以这里没有用
1.5 筛选的逻辑
表格数据中,最重要的一个功能就是要具有可筛选的能力,选出我所需要的信息,丢弃无用的信息。
下面我们还是用实战来学习pandas这个功能。
1.5.1 任务一: 我们以"Age"为筛选条件,显示年龄在10岁以下的乘客信息。
train[train["Age"]<10]
train.head()
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
1.5.2 任务二: 以"Age"为条件,将年龄在10岁以上和50岁以下的乘客信息显示出来,并将这个数据命名为midage
midage = train[(train["Age"]>=10) & (train["Age"]<50)]
midage.head()
| PassengerId | Survived | Pclass | Name | Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
【提示】了解pandas的条件筛选方式以及如何使用交集和并集操作
1.5.3 任务三:将midage的数据中第100行的"Pclass"和"Sex"的数据显示出来
midage.loc[[100],["Pclass","Sex"]]
【提示】在抽取数据中,我们希望数据的相对顺序保持不变,用什么函数可以达到这个效果呢? –使用reset_index()函数,将midage重置索引
1.5.4 任务四:使用loc方法将midage的数据中第100,105,108行的"Pclass","Name"和"Sex"的数据显示出来
midage.loc[[100,105,108],['Pclass','Name','Sex']]
| Pclass | Name | Sex |
---|
100 | 3 | Petranec, Miss. Matilda | female |
---|
105 | 3 | Mionoff, Mr. Stoytcho | male |
---|
108 | 3 | Rekic, Mr. Tido | male |
---|
1.5.5 任务五:使用iloc方法将midage的数据中第100,105,108行的"Pclass","Name"和"Sex"的数据显示出来
midage.iloc[[100,105,108],[2,3,4]]
| Pclass | Name | Sex |
---|
149 | 2 | Byles, Rev. Thomas Roussel Davids | male |
---|
160 | 3 | Cribb, Mr. John Hatfield | male |
---|
163 | 3 | Calic, Mr. Jovo | male |
---|
【思考】对比iloc 和loc 的异同
df.loc[行标签,列标签] 按照标签名称取数据 df.iloc[行位置,列位置] 按照位置取数据
1.6 了解你的数据吗?
教材《Python for Data Analysis》第五章
开始之前,导入numpy、pandas包和数据
import numpy as np
import pandas as pd
train_c = pd.read_csv("train_chinese.csv")
train_c.head()
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26.0 | 0 | 0 | STON/O2. 3101282 | 7.9250 | NaN | S |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35.0 | 1 | 0 | 113803 | 53.1000 | C123 | S |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry | male | 35.0 | 0 | 0 | 373450 | 8.0500 | NaN | S |
---|
1.6.1 任务一:利用Pandas对示例数据进行排序,要求升序
【代码解析】
pd.DataFrame() :创建一个DataFrame对象
np.arange(8).reshape((2, 4)) : 生成一个二维数组(2*4),第一列:0,1,2,3 第二列:4,5,6,7
index=['2, 1] :DataFrame 对象的索引列
columns=[‘d’, ‘a’, ‘b’, ‘c’] :DataFrame 对象的索引行
【问题】:大多数时候我们都是想根据列的值来排序,所以将你构建的DataFrame中的数据根据某一列,升序排列
df1 = pd.DataFrame(np.arange(8).reshape((2, 4)),index=[2,1],columns=['d', 'a', 'b', 'c'])
df1
df1.sort_values(by="c",ascending=True)
【思考】通过书本你能说出Pandas对DataFrame数据的其他排序方式吗?
【总结】下面将不同的排序方式做一个总结
1.让行索引升序排序
df1.sort_index()
2.让列索引升序排序
df1.sort_index(axis=1)
3.让列索引降序排序
df1.sort_index(axis=1,ascending=False)
4.让任选两列数据同时降序排序
df1.sort_values(by=["a","b"],ascending=False)
1.6.2 任务二:对泰坦尼克号数据(trian.csv)按票价和年龄两列进行综合排序(降序排列),从这个数据中你可以分析出什么?
'''
在开始我们已经导入了train_chinese.csv数据,而且前面我们也学习了导入数据过程,根据上面学习,我们直接对目标列进行排序即可
head(20) : 读取前20条数据
'''
train_c.sort_values(by=["票价","年龄"],ascending=False).head(20)
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
679 | 680 | 1 | 1 | Cardeza, Mr. Thomas Drake Martinez | male | 36.0 | 0 | 1 | PC 17755 | 512.3292 | B51 B53 B55 | C |
---|
258 | 259 | 1 | 1 | Ward, Miss. Anna | female | 35.0 | 0 | 0 | PC 17755 | 512.3292 | NaN | C |
---|
737 | 738 | 1 | 1 | Lesurer, Mr. Gustave J | male | 35.0 | 0 | 0 | PC 17755 | 512.3292 | B101 | C |
---|
438 | 439 | 0 | 1 | Fortune, Mr. Mark | male | 64.0 | 1 | 4 | 19950 | 263.0000 | C23 C25 C27 | S |
---|
341 | 342 | 1 | 1 | Fortune, Miss. Alice Elizabeth | female | 24.0 | 3 | 2 | 19950 | 263.0000 | C23 C25 C27 | S |
---|
88 | 89 | 1 | 1 | Fortune, Miss. Mabel Helen | female | 23.0 | 3 | 2 | 19950 | 263.0000 | C23 C25 C27 | S |
---|
27 | 28 | 0 | 1 | Fortune, Mr. Charles Alexander | male | 19.0 | 3 | 2 | 19950 | 263.0000 | C23 C25 C27 | S |
---|
742 | 743 | 1 | 1 | Ryerson, Miss. Susan Parker "Suzette" | female | 21.0 | 2 | 2 | PC 17608 | 262.3750 | B57 B59 B63 B66 | C |
---|
311 | 312 | 1 | 1 | Ryerson, Miss. Emily Borie | female | 18.0 | 2 | 2 | PC 17608 | 262.3750 | B57 B59 B63 B66 | C |
---|
299 | 300 | 1 | 1 | Baxter, Mrs. James (Helene DeLaudeniere Chaput) | female | 50.0 | 0 | 1 | PC 17558 | 247.5208 | B58 B60 | C |
---|
118 | 119 | 0 | 1 | Baxter, Mr. Quigg Edmond | male | 24.0 | 0 | 1 | PC 17558 | 247.5208 | B58 B60 | C |
---|
380 | 381 | 1 | 1 | Bidois, Miss. Rosalie | female | 42.0 | 0 | 0 | PC 17757 | 227.5250 | NaN | C |
---|
716 | 717 | 1 | 1 | Endres, Miss. Caroline Louise | female | 38.0 | 0 | 0 | PC 17757 | 227.5250 | C45 | C |
---|
700 | 701 | 1 | 1 | Astor, Mrs. John Jacob (Madeleine Talmadge Force) | female | 18.0 | 1 | 0 | PC 17757 | 227.5250 | C62 C64 | C |
---|
557 | 558 | 0 | 1 | Robbins, Mr. Victor | male | NaN | 0 | 0 | PC 17757 | 227.5250 | NaN | C |
---|
527 | 528 | 0 | 1 | Farthing, Mr. John | male | NaN | 0 | 0 | PC 17483 | 221.7792 | C95 | S |
---|
377 | 378 | 0 | 1 | Widener, Mr. Harry Elkins | male | 27.0 | 0 | 2 | 113503 | 211.5000 | C82 | C |
---|
779 | 780 | 1 | 1 | Robert, Mrs. Edward Scott (Elisabeth Walton Mc... | female | 43.0 | 0 | 1 | 24160 | 211.3375 | B3 | S |
---|
730 | 731 | 1 | 1 | Allen, Miss. Elisabeth Walton | female | 29.0 | 0 | 0 | 24160 | 211.3375 | B5 | S |
---|
689 | 690 | 1 | 1 | Madill, Miss. Georgette Alexandra | female | 15.0 | 0 | 1 | 24160 | 211.3375 | B5 | S |
---|
【思考】排序后,如果我们仅仅关注年龄和票价两列。根据常识我知道发现票价越高的应该客舱越好,所以我们会明显看出,票价前20的乘客中存活的有14人,这是相当高的一个比例,那么我们后面是不是可以进一步分析一下票价和存活之间的关系,年龄和存活之间的关系呢?当你开始发现数据之间的关系了,数据分析就开始了。 1、按照现有的交通工具,客舱越好舱内人均面积越高,逃生空间大,而且是否救生设备更多更靠近(如救生圈,救生衣,救生艇),需要进一步研究泰坦尼克号的结构 2、存活的乘客中年龄均在50及以下,青年中年人存活率是否更高,需要进一步分析每个年龄段(以十岁一个区间)的生存率,以及结合逃生时的具体情况(妇孺优先)
train_c.sort_values(by=["乘客等级"]).head(20)
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
445 | 446 | 1 | 1 | Dodge, Master. Washington | male | 4.00 | 0 | 2 | 33638 | 81.8583 | A34 | S |
---|
310 | 311 | 1 | 1 | Hays, Miss. Margaret Bechstein | female | 24.00 | 0 | 0 | 11767 | 83.1583 | C54 | C |
---|
309 | 310 | 1 | 1 | Francatelli, Miss. Laura Mabel | female | 30.00 | 0 | 0 | PC 17485 | 56.9292 | E36 | C |
---|
307 | 308 | 1 | 1 | Penasco y Castellana, Mrs. Victor de Satode (M... | female | 17.00 | 1 | 0 | PC 17758 | 108.9000 | C65 | C |
---|
306 | 307 | 1 | 1 | Fleming, Miss. Margaret | female | NaN | 0 | 0 | 17421 | 110.8833 | NaN | C |
---|
305 | 306 | 1 | 1 | Allison, Master. Hudson Trevor | male | 0.92 | 1 | 2 | 113781 | 151.5500 | C22 C26 | S |
---|
710 | 711 | 1 | 1 | Mayne, Mlle. Berthe Antonine ("Mrs de Villiers") | female | 24.00 | 0 | 0 | PC 17482 | 49.5042 | C90 | C |
---|
711 | 712 | 0 | 1 | Klaber, Mr. Herman | male | NaN | 0 | 0 | 113028 | 26.5500 | C124 | S |
---|
311 | 312 | 1 | 1 | Ryerson, Miss. Emily Borie | female | 18.00 | 2 | 2 | PC 17608 | 262.3750 | B57 B59 B63 B66 | C |
---|
712 | 713 | 1 | 1 | Taylor, Mr. Elmer Zebley | male | 48.00 | 1 | 0 | 19996 | 52.0000 | C126 | S |
---|
298 | 299 | 1 | 1 | Saalfeld, Mr. Adolphe | male | NaN | 0 | 0 | 19988 | 30.5000 | C106 | S |
---|
297 | 298 | 0 | 1 | Allison, Miss. Helen Loraine | female | 2.00 | 1 | 2 | 113781 | 151.5500 | C22 C26 | S |
---|
295 | 296 | 0 | 1 | Lewy, Mr. Ervin G | male | NaN | 0 | 0 | PC 17612 | 27.7208 | NaN | C |
---|
716 | 717 | 1 | 1 | Endres, Miss. Caroline Louise | female | 38.00 | 0 | 0 | PC 17757 | 227.5250 | C45 | C |
---|
291 | 292 | 1 | 1 | Bishop, Mrs. Dickinson H (Helen Walton) | female | 19.00 | 1 | 0 | 11967 | 91.0792 | B49 | C |
---|
290 | 291 | 1 | 1 | Barber, Miss. Ellen "Nellie" | female | 26.00 | 0 | 0 | 19877 | 78.8500 | NaN | S |
---|
571 | 572 | 1 | 1 | Appleton, Mrs. Edward Dale (Charlotte Lamson) | female | 53.00 | 2 | 0 | 11769 | 51.4792 | C101 | S |
---|
299 | 300 | 1 | 1 | Baxter, Mrs. James (Helene DeLaudeniere Chaput) | female | 50.00 | 0 | 1 | PC 17558 | 247.5208 | B58 B60 | C |
---|
284 | 285 | 0 | 1 | Smith, Mr. Richard William | male | NaN | 0 | 0 | 113056 | 26.0000 | A19 | S |
---|
708 | 709 | 1 | 1 | Cleaver, Miss. Alice | female | 22.00 | 0 | 0 | 113781 | 151.5500 | NaN | S |
---|
train_c.sort_values(by=["乘客等级"],ascending=False).head(20)
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
511 | 512 | 0 | 3 | Webber, Mr. James | male | NaN | 0 | 0 | SOTON/OQ 3101316 | 8.0500 | NaN | S |
---|
500 | 501 | 0 | 3 | Calic, Mr. Petar | male | 17.0 | 0 | 0 | 315086 | 8.6625 | NaN | S |
---|
501 | 502 | 0 | 3 | Canavan, Miss. Mary | female | 21.0 | 0 | 0 | 364846 | 7.7500 | NaN | Q |
---|
502 | 503 | 0 | 3 | O'Sullivan, Miss. Bridget Mary | female | NaN | 0 | 0 | 330909 | 7.6292 | NaN | Q |
---|
503 | 504 | 0 | 3 | Laitinen, Miss. Kristina Sofia | female | 37.0 | 0 | 0 | 4135 | 9.5875 | NaN | S |
---|
508 | 509 | 0 | 3 | Olsen, Mr. Henry Margido | male | 28.0 | 0 | 0 | C 4001 | 22.5250 | NaN | S |
---|
509 | 510 | 1 | 3 | Lang, Mr. Fang | male | 26.0 | 0 | 0 | 1601 | 56.4958 | NaN | S |
---|
510 | 511 | 1 | 3 | Daly, Mr. Eugene Patrick | male | 29.0 | 0 | 0 | 382651 | 7.7500 | NaN | Q |
---|
514 | 515 | 0 | 3 | Coleff, Mr. Satio | male | 24.0 | 0 | 0 | 349209 | 7.4958 | NaN | S |
---|
532 | 533 | 0 | 3 | Elias, Mr. Joseph Jr | male | 17.0 | 1 | 1 | 2690 | 7.2292 | NaN | C |
---|
517 | 518 | 0 | 3 | Ryan, Mr. Patrick | male | NaN | 0 | 0 | 371110 | 24.1500 | NaN | Q |
---|
519 | 520 | 0 | 3 | Pavlovic, Mr. Stefo | male | 32.0 | 0 | 0 | 349242 | 7.8958 | NaN | S |
---|
521 | 522 | 0 | 3 | Vovk, Mr. Janko | male | 22.0 | 0 | 0 | 349252 | 7.8958 | NaN | S |
---|
522 | 523 | 0 | 3 | Lahoud, Mr. Sarkis | male | NaN | 0 | 0 | 2624 | 7.2250 | NaN | C |
---|
524 | 525 | 0 | 3 | Kassem, Mr. Fared | male | NaN | 0 | 0 | 2700 | 7.2292 | NaN | C |
---|
525 | 526 | 0 | 3 | Farrell, Mr. James | male | 40.5 | 0 | 0 | 367232 | 7.7500 | NaN | Q |
---|
528 | 529 | 0 | 3 | Salonen, Mr. Johan Werner | male | 39.0 | 0 | 0 | 3101296 | 7.9250 | NaN | S |
---|
499 | 500 | 0 | 3 | Svensson, Mr. Olof | male | 24.0 | 0 | 0 | 350035 | 7.7958 | NaN | S |
---|
497 | 498 | 0 | 3 | Shellard, Mr. Frederick William | male | NaN | 0 | 0 | C.A. 6212 | 15.1000 | NaN | S |
---|
train_c.sort_values(by=["年龄"]).head(20)
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
803 | 804 | 1 | 3 | Thomas, Master. Assad Alexander | male | 0.42 | 0 | 1 | 2625 | 8.5167 | NaN | C |
---|
755 | 756 | 1 | 2 | Hamalainen, Master. Viljo | male | 0.67 | 1 | 1 | 250649 | 14.5000 | NaN | S |
---|
644 | 645 | 1 | 3 | Baclini, Miss. Eugenie | female | 0.75 | 2 | 1 | 2666 | 19.2583 | NaN | C |
---|
469 | 470 | 1 | 3 | Baclini, Miss. Helene Barbara | female | 0.75 | 2 | 1 | 2666 | 19.2583 | NaN | C |
---|
78 | 79 | 1 | 2 | Caldwell, Master. Alden Gates | male | 0.83 | 0 | 2 | 248738 | 29.0000 | NaN | S |
---|
831 | 832 | 1 | 2 | Richards, Master. George Sibley | male | 0.83 | 1 | 1 | 29106 | 18.7500 | NaN | S |
---|
305 | 306 | 1 | 1 | Allison, Master. Hudson Trevor | male | 0.92 | 1 | 2 | 113781 | 151.5500 | C22 C26 | S |
---|
827 | 828 | 1 | 2 | Mallet, Master. Andre | male | 1.00 | 0 | 2 | S.C./PARIS 2079 | 37.0042 | NaN | C |
---|
381 | 382 | 1 | 3 | Nakid, Miss. Maria ("Mary") | female | 1.00 | 0 | 2 | 2653 | 15.7417 | NaN | C |
---|
164 | 165 | 0 | 3 | Panula, Master. Eino Viljami | male | 1.00 | 4 | 1 | 3101295 | 39.6875 | NaN | S |
---|
183 | 184 | 1 | 2 | Becker, Master. Richard F | male | 1.00 | 2 | 1 | 230136 | 39.0000 | F4 | S |
---|
386 | 387 | 0 | 3 | Goodwin, Master. Sidney Leonard | male | 1.00 | 5 | 2 | CA 2144 | 46.9000 | NaN | S |
---|
172 | 173 | 1 | 3 | Johnson, Miss. Eleanor Ileen | female | 1.00 | 1 | 1 | 347742 | 11.1333 | NaN | S |
---|
788 | 789 | 1 | 3 | Dean, Master. Bertram Vere | male | 1.00 | 1 | 2 | C.A. 2315 | 20.5750 | NaN | S |
---|
642 | 643 | 0 | 3 | Skoog, Miss. Margit Elizabeth | female | 2.00 | 3 | 2 | 347088 | 27.9000 | NaN | S |
---|
7 | 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2.00 | 3 | 1 | 349909 | 21.0750 | NaN | S |
---|
530 | 531 | 1 | 2 | Quick, Miss. Phyllis May | female | 2.00 | 1 | 1 | 26360 | 26.0000 | NaN | S |
---|
297 | 298 | 0 | 1 | Allison, Miss. Helen Loraine | female | 2.00 | 1 | 2 | 113781 | 151.5500 | C22 C26 | S |
---|
824 | 825 | 0 | 3 | Panula, Master. Urho Abraham | male | 2.00 | 4 | 1 | 3101295 | 39.6875 | NaN | S |
---|
205 | 206 | 0 | 3 | Strom, Miss. Telma Matilda | female | 2.00 | 0 | 1 | 347054 | 10.4625 | G6 | S |
---|
1.6.3 任务三:利用Pandas进行算术计算,计算两个DataFrame数据相加结果
frame1_a = pd.DataFrame(np.arange(9.).reshape(3, 3),
columns=['a', 'b', 'c'],
index=['one', 'two', 'three'])
frame1_b = pd.DataFrame(np.arange(12.).reshape(4, 3),
columns=['a', 'e', 'c'],
index=['first', 'one', 'two', 'second'])
print(frame1_a)
print(frame1_b)
a b c
one 0.0 1.0 2.0
two 3.0 4.0 5.0
three 6.0 7.0 8.0
a e c
first 0.0 1.0 2.0
one 3.0 4.0 5.0
two 6.0 7.0 8.0
second 9.0 10.0 11.0
将frame_a和frame_b进行相加
frame1_a + frame1_b
| a | b | c | e |
---|
first | NaN | NaN | NaN | NaN |
---|
one | 3.0 | NaN | 7.0 | NaN |
---|
second | NaN | NaN | NaN | NaN |
---|
three | NaN | NaN | NaN | NaN |
---|
two | 9.0 | NaN | 13.0 | NaN |
---|
【提醒】两个DataFrame相加后,会返回一个新的DataFrame,对应的行和列的值会相加,没有对应的会变成空值NaN。
当然,DataFrame还有很多算术运算,如减法,除法等,有兴趣的同学可以看《利用Python进行数据分析》第五章 算术运算与数据对齐 部分,多在网络上查找相关学习资料。
1.6.4 任务四:通过泰坦尼克号数据如何计算出在船上最大的家族有多少人?
'''
还是用之前导入的chinese_train.csv如果我们想看看在船上,最大的家族有多少人(‘兄弟姐妹个数’+‘父母子女个数’),我们该怎么做呢?
'''
max(train_c["堂兄弟/妹个数"] + train_c["父母子女个数"])
10
【提醒】我们只需找出”兄弟姐妹个数“和”父母子女个数“之和最大的数,当然你还可以想出很多方法和思考角度,欢迎你来说出你的看法。
train_c.sort_values(by=['父母子女个数','堂兄弟/妹个数'],ascending=False).head()
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
678 | 679 | 0 | 3 | Goodwin, Mrs. Frederick (Augusta Tyler) | female | 43.0 | 1 | 6 | CA 2144 | 46.9000 | NaN | S |
---|
13 | 14 | 0 | 3 | Andersson, Mr. Anders Johan | male | 39.0 | 1 | 5 | 347082 | 31.2750 | NaN | S |
---|
25 | 26 | 1 | 3 | Asplund, Mrs. Carl Oscar (Selma Augusta Emilia... | female | 38.0 | 1 | 5 | 347077 | 31.3875 | NaN | S |
---|
610 | 611 | 0 | 3 | Andersson, Mrs. Anders Johan (Alfrida Konstant... | female | 39.0 | 1 | 5 | 347082 | 31.2750 | NaN | S |
---|
638 | 639 | 0 | 3 | Panula, Mrs. Juha (Maria Emilia Ojala) | female | 41.0 | 0 | 5 | 3101295 | 39.6875 | NaN | S |
---|
train_c.sort_values(by=['堂兄弟/妹个数','父母子女个数'],ascending=False).head()
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
159 | 160 | 0 | 3 | Sage, Master. Thomas Henry | male | NaN | 8 | 2 | CA. 2343 | 69.55 | NaN | S |
---|
180 | 181 | 0 | 3 | Sage, Miss. Constance Gladys | female | NaN | 8 | 2 | CA. 2343 | 69.55 | NaN | S |
---|
201 | 202 | 0 | 3 | Sage, Mr. Frederick | male | NaN | 8 | 2 | CA. 2343 | 69.55 | NaN | S |
---|
324 | 325 | 0 | 3 | Sage, Mr. George John Jr | male | NaN | 8 | 2 | CA. 2343 | 69.55 | NaN | S |
---|
792 | 793 | 0 | 3 | Sage, Miss. Stella Anna | female | NaN | 8 | 2 | CA. 2343 | 69.55 | NaN | S |
---|
1.6.5 任务五:学会使用Pandas describe()函数查看数据基本统计信息
frame2 = pd.DataFrame([[1.4, np.nan],
[7.1, -4.5],
[np.nan, np.nan],
[0.75, -1.3]
], index=['a', 'b', 'c', 'd'], columns=['one', 'two'])
frame2
| one | two |
---|
a | 1.40 | NaN |
---|
b | 7.10 | -4.5 |
---|
c | NaN | NaN |
---|
d | 0.75 | -1.3 |
---|
调用 describe 函数,观察frame2的数据基本信息
frame2.describe()
| one | two |
---|
count | 3.000000 | 2.000000 |
---|
mean | 3.083333 | -2.900000 |
---|
std | 3.493685 | 2.262742 |
---|
min | 0.750000 | -4.500000 |
---|
25% | 1.075000 | -3.700000 |
---|
50% | 1.400000 | -2.900000 |
---|
75% | 4.250000 | -2.100000 |
---|
max | 7.100000 | -1.300000 |
---|
1.6.6 任务六:分别看看泰坦尼克号数据集中 票价、父母子女 这列数据的基本统计数据,你能发现什么?
train_c[["票价","父母子女个数"]].describe()
| 票价 | 父母子女个数 |
---|
count | 891.000000 | 891.000000 |
---|
mean | 32.204208 | 0.381594 |
---|
std | 49.693429 | 0.806057 |
---|
min | 0.000000 | 0.000000 |
---|
25% | 7.910400 | 0.000000 |
---|
50% | 14.454200 | 0.000000 |
---|
75% | 31.000000 | 0.000000 |
---|
max | 512.329200 | 6.000000 |
---|
【思考】从上面数据我们可以看出, 票价数据共有891个,平均值32.20,标准差约49.69,票价波动大,25%的人的票价低于7.91,50%的人的票价低于14.45,75%的人的票价低于31.00,票价最大值512.33,最小值0
多做几个组数据的统计,看看你能分析出什么?
'''
describe()默认只对数字类型的数据,生成描述性统计,总结数据集分布的中心趋势,分散和形状,不包括NaN值
describe(include=[object])则只对对象类型的数据(例如字符串或时间),生成描述性统计
describe(include="all")对所有数据类型,生成对应的描述性统计
'''
train_c.describe(include="all")
| 乘客ID | 是否幸存 | 乘客等级 | 姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 |
---|
count | 891.000000 | 891.000000 | 891.000000 | 891 | 891 | 714.000000 | 891.000000 | 891.000000 | 891 | 891.000000 | 204 | 889 |
---|
unique | NaN | NaN | NaN | 891 | 2 | NaN | NaN | NaN | 681 | NaN | 147 | 3 |
---|
top | NaN | NaN | NaN | Minahan, Dr. William Edward | male | NaN | NaN | NaN | CA. 2343 | NaN | C23 C25 C27 | S |
---|
freq | NaN | NaN | NaN | 1 | 577 | NaN | NaN | NaN | 7 | NaN | 4 | 644 |
---|
mean | 446.000000 | 0.383838 | 2.308642 | NaN | NaN | 29.699118 | 0.523008 | 0.381594 | NaN | 32.204208 | NaN | NaN |
---|
std | 257.353842 | 0.486592 | 0.836071 | NaN | NaN | 14.526497 | 1.102743 | 0.806057 | NaN | 49.693429 | NaN | NaN |
---|
min | 1.000000 | 0.000000 | 1.000000 | NaN | NaN | 0.420000 | 0.000000 | 0.000000 | NaN | 0.000000 | NaN | NaN |
---|
25% | 223.500000 | 0.000000 | 2.000000 | NaN | NaN | 20.125000 | 0.000000 | 0.000000 | NaN | 7.910400 | NaN | NaN |
---|
50% | 446.000000 | 0.000000 | 3.000000 | NaN | NaN | 28.000000 | 0.000000 | 0.000000 | NaN | 14.454200 | NaN | NaN |
---|
75% | 668.500000 | 1.000000 | 3.000000 | NaN | NaN | 38.000000 | 1.000000 | 0.000000 | NaN | 31.000000 | NaN | NaN |
---|
max | 891.000000 | 1.000000 | 3.000000 | NaN | NaN | 80.000000 | 8.000000 | 6.000000 | NaN | 512.329200 | NaN | NaN |
---|
【总结】本节中我们通过Pandas的一些内置函数对数据进行了初步统计查看,构建自己的数据分析思维,这也是第一章最重要的点,对数据有个基本认识,了解自己在做什么,为什么这么做,后面的章节将开始对数据进行清洗,进一步分析。
|