1 载入数据和数据观察
1.1导入numpy和pandas
import numpy as np
import pandas as pd
1.2 载入数据
(1) 使用相对路径载入数据
data=pd.read_csv('train.csv') #相对路径
data.head() #head()默认是前5行
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 |
(2) 使用绝对路径载入数据
data=pd.read_csv("D:/Jupyter NoteBook/组队学习/hands-on-data-analysis-master/第一单元项目集合/train.csv")
data.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’的不同,如何加载这两个数据集?
1)read_table是以制表符 \t 作为数据的标志,以行为单位进行存储;read_csv是以分隔符号 ‘,’ 作为数据的标志。使参数 sep=‘,’ 就能让他们效果一样。
2 ) TSV:tab separated values;即“制表符分隔值”,CSV: comma separated values;即“逗号分隔值”
使用pd.read_table()来读取数据
data=pd.read_table('train.csv',sep=',')
data.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.3每1000行为一个数据模块,逐块读取
chunks=pd.read_csv('train.csv',chunksize=1000)
chunks
<pandas.io.parsers.TextFileReader at 0x20b2ef42490>
【思考】什么是逐块读取?为什么要逐块读取呢?
【提示】大家可以chunker(数据块)是什么类型?用for
循环打印出来出处具体的样子是什么?
使用read_csv会把整个文件的数据读取到DataFrame中,当数据量大时,就会很吃内存;所以在read_csv中通过设置参数chunksize来指定一个chunksize分块大小来读取文件,它会返回一个可迭代的对象TextFileReader,然后使用for循环取出数据。
chunks=pd.read_csv('train.csv',chunksize=500) #该数据集不到1000,这里使用500
for chunk in chunks:
print(chunk)
PassengerId Survived Pclass \
0 1 0 3
1 2 1 1
2 3 1 3
3 4 1 1
4 5 0 3
.. ... ... ...
495 496 0 3
496 497 1 1
497 498 0 3
498 499 0 1
499 500 0 3
Name Sex Age SibSp \
0 Braund, Mr. Owen Harris male 22.0 1
1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1
2 Heikkinen, Miss. Laina female 26.0 0
3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1
4 Allen, Mr. William Henry male 35.0 0
.. ... ... ... ...
495 Yousseff, Mr. Gerious male NaN 0
496 Eustis, Miss. Elizabeth Mussey female 54.0 1
497 Shellard, Mr. Frederick William male NaN 0
498 Allison, Mrs. Hudson J C (Bessie Waldo Daniels) female 25.0 1
499 Svensson, Mr. Olof male 24.0 0
Parch Ticket Fare Cabin Embarked
0 0 A/5 21171 7.2500 NaN S
1 0 PC 17599 71.2833 C85 C
2 0 STON/O2. 3101282 7.9250 NaN S
3 0 113803 53.1000 C123 S
4 0 373450 8.0500 NaN S
.. ... ... ... ... ...
495 0 2627 14.4583 NaN C
496 0 36947 78.2667 D20 C
497 0 C.A. 6212 15.1000 NaN S
498 2 113781 151.5500 C22 C26 S
499 0 350035 7.7958 NaN S
[500 rows x 12 columns]
PassengerId Survived Pclass Name \
500 501 0 3 Calic, Mr. Petar
501 502 0 3 Canavan, Miss. Mary
502 503 0 3 O'Sullivan, Miss. Bridget Mary
503 504 0 3 Laitinen, Miss. Kristina Sofia
504 505 1 1 Maioni, Miss. Roberta
.. ... ... ... ...
886 887 0 2 Montvila, Rev. Juozas
887 888 1 1 Graham, Miss. Margaret Edith
888 889 0 3 Johnston, Miss. Catherine Helen "Carrie"
889 890 1 1 Behr, Mr. Karl Howell
890 891 0 3 Dooley, Mr. Patrick
Sex Age SibSp Parch Ticket Fare Cabin Embarked
500 male 17.0 0 0 315086 8.6625 NaN S
501 female 21.0 0 0 364846 7.7500 NaN Q
502 female NaN 0 0 330909 7.6292 NaN Q
503 female 37.0 0 0 4135 9.5875 NaN S
504 female 16.0 0 0 110152 86.5000 B79 S
.. ... ... ... ... ... ... ... ...
886 male 27.0 0 0 211536 13.0000 NaN S
887 female 19.0 0 0 112053 30.0000 B42 S
888 female NaN 1 2 W./C. 6607 23.4500 NaN S
889 male 26.0 0 0 111369 30.0000 C148 C
890 male 32.0 0 0 370376 7.7500 NaN Q
[391 rows x 12 columns]
1.4 修改表头
PassengerId => 乘客ID
Survived => 是否幸存
Pclass => 乘客等级(1/2/3等舱位)
Name => 乘客姓名
Sex => 性别
Age => 年龄
SibSp => 堂兄弟/妹个数
Parch => 父母与小孩个数
Ticket => 船票信息
Fare => 票价
Cabin => 客舱
Embarked => 登船港口
#查看数据信息
data=pd.read_csv('train.csv')
data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 PassengerId 891 non-null int64
1 Survived 891 non-null int64
2 Pclass 891 non-null int64
3 Name 891 non-null object
4 Sex 891 non-null object
5 Age 714 non-null float64
6 SibSp 891 non-null int64
7 Parch 891 non-null int64
8 Ticket 891 non-null object
9 Fare 891 non-null float64
10 Cabin 204 non-null object
11 Embarked 889 non-null object
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB
data.columns=['乘客ID','是否幸存', '乘客等级', '乘客姓名','性别','年龄',
'堂兄弟/妹个数','父母与小孩个数','船票信息','票价','客舱','登船港口']
data_=data.set_index("乘客ID")
data_.head(2)
是否幸存 | 乘客等级 | 乘客姓名 | 性别 | 年龄 | 堂兄弟/妹个数 | 父母与小孩个数 | 船票信息 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|---|---|---|
乘客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 |
data_1= pd.read_csv('train.csv',
names=['乘客ID','是否幸存','乘客等级','乘客姓名','性别',
'年龄','兄弟姐妹个数','父母子女个数','船票信息','票价','客舱','登船港口'],
index_col='乘客ID',header=0)
data_1.head(3)
是否幸存 | 乘客等级 | 乘客姓名 | 性别 | 年龄 | 兄弟姐妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|---|---|---|
乘客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 |
【思考】所谓将表头改为中文其中一个思路是:将英文列名表头替换成中文。还有其他的方法吗?
data_1.rename(columns=('PassengerId': '乘客ID', 'Survived': '是否幸存',
'Pclass': '乘客等级(1/2/3等舱位)', 'Name': '乘客姓名', 'Sex': '性别',
'Age':'年龄','SibSp':'堂兄弟/妹个数','Parch':'父母与小孩个数',
'Ticket':'船票信息','Fare':'票价','Cabin':'客舱','Embarked':'登船港口' },
inplace=True)
1.5 查看数据的基本信息
data_1.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 891 entries, 1 to 891
Data columns (total 11 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 是否幸存 891 non-null int64
1 乘客等级 891 non-null int64
2 乘客姓名 891 non-null object
3 性别 891 non-null object
4 年龄 714 non-null float64
5 兄弟姐妹个数 891 non-null int64
6 父母子女个数 891 non-null int64
7 船票信息 891 non-null object
8 票价 891 non-null float64
9 客舱 204 non-null object
10 登船港口 889 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 123.5+ KB
1.6查看表格前10行的数据和后15行的数据
#前10行数据
data_1.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 |
#后15行数据
data_1.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.7 查看数据的缺失情况
#查看每类标签数据的缺失量
data_1.isnull().sum()
是否幸存 0
乘客等级 0
乘客姓名 0
性别 0
年龄 177
兄弟姐妹个数 0
父母子女个数 0
船票信息 0
票价 0
客舱 687
登船港口 2
dtype: int64
#有空的地方返回false
data_1.notnull().head()
是否幸存 | 乘客等级 | 乘客姓名 | 性别 | 年龄 | 兄弟姐妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|---|---|---|
乘客ID | |||||||||||
1 | True | True | True | True | True | True | True | True | True | False | True |
2 | True | True | True | True | True | True | True | True | True | True | True |
3 | True | True | True | True | True | True | True | True | True | False | True |
4 | True | True | True | True | True | True | True | True | True | True | True |
5 | True | True | True | True | True | True | True | True | True | False | True |
#判断数据是否为空,为空的地方返回True,其余地方返回False
data_1.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.8 保存数据
# 注意:不同的操作系统保存下来可能会有乱码。可以加入`encoding='GBK' 或者 ’encoding = ’uft-8‘‘`
data_1.to_csv('train_chinese.csv',encoding='GBK')
2 pandas基础
开始前导入numpy和pandas
import numpy as np
import pandas as pd
2.1 DateFrame和Series类型
#Series的创建
name_ages={'张三': 35, '李四': 42, '王二麻子': 25, '李华': 15}
example_1=pd.Series(name_ages)
example_1
张三 35
李四 42
王二麻子 25
李华 15
dtype: int64
#DataFrame的创建
data = {'name': ['张三', '李四', '王二麻子', '李华'],
'ages': [35, 42, 25, 15],
'height': [170,165,175,180]
}
example_2 = pd.DataFrame(data)
example_2
name | ages | height | |
---|---|---|---|
0 | 张三 | 35 | 170 |
1 | 李四 | 42 | 165 |
2 | 王二麻子 | 25 | 175 |
3 | 李华 | 15 | 180 |
#删除height列
example_2=example_2.drop(labels='height',axis=1)
example_2
name | ages | |
---|---|---|
0 | 张三 | 35 |
1 | 李四 | 42 |
2 | 王二麻子 | 25 |
3 | 李华 | 15 |
2.2查看DataFrame数据的每列的名称
#加载数据
data=pd.read_csv('train_chinese.csv',encoding='GBK')
data.head(3)
乘客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 |
#查看表头
data.columns
Index(['乘客ID', '是否幸存', '乘客等级', '乘客姓名', '性别', '年龄', '兄弟姐妹个数', '父母子女个数', '船票信息',
'票价', '客舱', '登船港口'],
dtype='object')
2.3 查看相关列的列名
#查看客舱,法一
data['客舱'].head()
0 NaN
1 C85
2 NaN
3 C123
4 NaN
Name: 客舱, dtype: object
#查看客舱,法二
data.客舱.head()
0 NaN
1 C85
2 NaN
3 C123
4 NaN
Name: 客舱, dtype: object
2.4 对比"test_1.csv"和"train.csv",将"test_1.csv"多出的列删除
data_1=pd.read_csv('test_1.csv')
data_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 |
#删除多余的列a
del data_1['a']
data_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 |
【思考】还有其他的删除多余的列的方式吗?
思考回答
data_1.drop(labels='a',axis=1)
2.5 将[‘PassengerId’,‘Name’,‘Age’,‘Ticket’]这几个列元素隐藏
data.drop(['乘客ID','乘客姓名','年龄','船票信息'],axis=1).head()
是否幸存 | 乘客等级 | 性别 | 兄弟姐妹个数 | 父母子女个数 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|
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 |
【思考】对比任务五和任务六,是不是使用了不一样的方法(函数),如果使用一样的函数如何完成上面的不同的要求呢?
【思考回答】
如果想要完全的删除你的数据结构,使用inplace=True,因为使用inplace就将原数据覆盖了,所以这里没有用
2.6 筛选的逻辑
表格数据中,最重要的一个功能就是要具有可筛选的能力,选出所需要的信息,丢弃无用的信息。
2.6.1 筛选"Age"在10岁以下的乘客信息
data[data.年龄<10].head()
乘客ID | 是否幸存 | 乘客等级 | 乘客姓名 | 性别 | 年龄 | 兄弟姐妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
7 | 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2.0 | 3 | 1 | 349909 | 21.0750 | NaN | S |
10 | 11 | 1 | 3 | Sandstrom, Miss. Marguerite Rut | female | 4.0 | 1 | 1 | PP 9549 | 16.7000 | G6 | S |
16 | 17 | 0 | 3 | Rice, Master. Eugene | male | 2.0 | 4 | 1 | 382652 | 29.1250 | NaN | Q |
24 | 25 | 0 | 3 | Palsson, Miss. Torborg Danira | female | 8.0 | 3 | 1 | 349909 | 21.0750 | NaN | S |
43 | 44 | 1 | 2 | Laroche, Miss. Simonne Marie Anne Andree | female | 3.0 | 1 | 2 | SC/Paris 2123 | 41.5792 | NaN | C |
2.6.2 筛选"Age"在10岁以上50岁以下的乘客信息
midage=data[(data.年龄>10) & (data.年龄<50)]
midage.head(3)
乘客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 |
2.6.3 显示midage的数据中第100行的"Pclass"和"Sex"的数据
midage.reset_index(drop=True).loc[[100],['乘客等级','性别']]
乘客等级 | 性别 | |
---|---|---|
100 | 2 | male |
【提示】在抽取数据中,我们希望数据的相对顺序保持不变,用什么函数可以达到这个效果呢?
使用reset_index函数重置索引,参数drop,False表示重新设置索引后将原索引作为新的一列并入DataFrame,True表示删除原索引
2.6.4 用loc方法抽取midage的数据中第100,105,108行的"Pclass","Name"和"Sex"的数据
midage.reset_index(drop=True).loc[[100,105,108],['乘客等级','乘客姓名','性别']]
乘客等级 | 乘客姓名 | 性别 | |
---|---|---|---|
100 | 2 | Byles, Rev. Thomas Roussel Davids | male |
105 | 3 | Cribb, Mr. John Hatfield | male |
108 | 3 | Calic, Mr. Jovo | male |
2.6.5 使用iloc方法抽取midage的数据中第100,105,108行的"Pclass","Name"和"Sex"的数据
midage.reset_index(drop=True).iloc[[100,105,108],[2,4,5]]
乘客等级 | 性别 | 年龄 | |
---|---|---|---|
100 | 2 | male | 42.0 |
105 | 3 | male | 44.0 |
108 | 3 | male | 17.0 |
【思考】对比iloc
和loc
的异同
iloc和loc的索引方式不同
3 探索性数据分析
导入numpy、pandas包和数据
import numpy as np
import pandas as pd
#载入train_chinese.csv数据
data=pd.read_csv('train_chinese.csv',encoding='GBK')
data.head(3)
乘客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.1 构建一个的DataFrame数据
test = pd.DataFrame(np.arange(8).reshape((2, 4)),
index=['2', '1'],
columns=['d', 'a', 'b', 'c'])
test
d | a | b | c | |
---|---|---|---|---|
2 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
【问题】:大多数时候我们都是想根据列的值来排序,所以将你构建的DataFrame中的数据根据某一列,升序排列
#根据d列进行升序排列
test.sort_values(by='d',ascending=True) #ascending,默认为True,即升序排列
d | a | b | c | |
---|---|---|---|---|
2 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
1.让行索引升序排序
test.sort_index()
d | a | b | c | |
---|---|---|---|---|
1 | 4 | 5 | 6 | 7 |
2 | 0 | 1 | 2 | 3 |
2.让列索引升序排序
test.sort_index(axis=1)
a | b | c | d | |
---|---|---|---|---|
2 | 1 | 2 | 3 | 0 |
1 | 5 | 6 | 7 | 4 |
3.让列索引降序排序
test.sort_index(axis=1,ascending=False) #ascending=False,降序排列
d | c | b | a | |
---|---|---|---|---|
2 | 0 | 3 | 2 | 1 |
1 | 4 | 7 | 6 | 5 |
4.让任选两列数据同时降序排序
test.sort_values(by=['a','b'])
d | a | b | c | |
---|---|---|---|---|
2 | 0 | 1 | 2 | 3 |
1 | 4 | 5 | 6 | 7 |
3.2 按票价和年龄两列进行综合排序(降序排列)
data.sort_values(by=['票价','年龄'],ascending=False).head()
乘客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 |
多做几个数据的排序
#以年龄和是否幸存降序排列
data.sort_values(by=['年龄','是否幸存'],ascending=False).head(20)
乘客ID | 是否幸存 | 乘客等级 | 乘客姓名 | 性别 | 年龄 | 兄弟姐妹个数 | 父母子女个数 | 船票信息 | 票价 | 客舱 | 登船港口 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
630 | 631 | 1 | 1 | Barkworth, Mr. Algernon Henry Wilson | male | 80.0 | 0 | 0 | 27042 | 30.0000 | A23 | S |
851 | 852 | 0 | 3 | Svensson, Mr. Johan | male | 74.0 | 0 | 0 | 347060 | 7.7750 | NaN | S |
96 | 97 | 0 | 1 | Goldschmidt, Mr. George B | male | 71.0 | 0 | 0 | PC 17754 | 34.6542 | A5 | C |
493 | 494 | 0 | 1 | Artagaveytia, Mr. Ramon | male | 71.0 | 0 | 0 | PC 17609 | 49.5042 | NaN | C |
116 | 117 | 0 | 3 | Connors, Mr. Patrick | male | 70.5 | 0 | 0 | 370369 | 7.7500 | NaN | Q |
672 | 673 | 0 | 2 | Mitchell, Mr. Henry Michael | male | 70.0 | 0 | 0 | C.A. 24580 | 10.5000 | NaN | S |
745 | 746 | 0 | 1 | Crosby, Capt. Edward Gifford | male | 70.0 | 1 | 1 | WE/P 5735 | 71.0000 | B22 | S |
33 | 34 | 0 | 2 | Wheadon, Mr. Edward H | male | 66.0 | 0 | 0 | C.A. 24579 | 10.5000 | NaN | S |
54 | 55 | 0 | 1 | Ostby, Mr. Engelhart Cornelius | male | 65.0 | 0 | 1 | 113509 | 61.9792 | B30 | C |
280 | 281 | 0 | 3 | Duane, Mr. Frank | male | 65.0 | 0 | 0 | 336439 | 7.7500 | NaN | Q |
456 | 457 | 0 | 1 | Millet, Mr. Francis Davis | male | 65.0 | 0 | 0 | 13509 | 26.5500 | E38 | S |
438 | 439 | 0 | 1 | Fortune, Mr. Mark | male | 64.0 | 1 | 4 | 19950 | 263.0000 | C23 C25 C27 | S |
545 | 546 | 0 | 1 | Nicholson, Mr. Arthur Ernest | male | 64.0 | 0 | 0 | 693 | 26.0000 | NaN | S |
275 | 276 | 1 | 1 | Andrews, Miss. Kornelia Theodosia | female | 63.0 | 1 | 0 | 13502 | 77.9583 | D7 | S |
483 | 484 | 1 | 3 | Turkula, Mrs. (Hedwig) | female | 63.0 | 0 | 0 | 4134 | 9.5875 | NaN | S |
570 | 571 | 1 | 2 | Harris, Mr. George | male | 62.0 | 0 | 0 | S.W./PP 752 | 10.5000 | NaN | S |
829 | 830 | 1 | 1 | Stone, Mrs. George Nelson (Martha Evelyn) | female | 62.0 | 0 | 0 | 113572 | 80.0000 | B28 | NaN |
252 | 253 | 0 | 1 | Stead, Mr. William Thomas | male | 62.0 | 0 | 0 | 113514 | 26.5500 | C87 | S |
555 | 556 | 0 | 1 | Wright, Mr. George | male | 62.0 | 0 | 0 | 113807 | 26.5500 | NaN | S |
170 | 171 | 0 | 1 | Van der hoef, Mr. Wyckoff | male | 61.0 | 0 | 0 | 111240 | 33.5000 | B19 | S |
#以年龄和是否幸存升序排列
data.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 |
469 | 470 | 1 | 3 | Baclini, Miss. Helene Barbara | female | 0.75 | 2 | 1 | 2666 | 19.2583 | NaN | C |
644 | 645 | 1 | 3 | Baclini, Miss. Eugenie | 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 |
164 | 165 | 0 | 3 | Panula, Master. Eino Viljami | male | 1.00 | 4 | 1 | 3101295 | 39.6875 | NaN | 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 |
183 | 184 | 1 | 2 | Becker, Master. Richard F | male | 1.00 | 2 | 1 | 230136 | 39.0000 | F4 | S |
381 | 382 | 1 | 3 | Nakid, Miss. Maria ("Mary") | female | 1.00 | 0 | 2 | 2653 | 15.7417 | NaN | C |
788 | 789 | 1 | 3 | Dean, Master. Bertram Vere | male | 1.00 | 1 | 2 | C.A. 2315 | 20.5750 | NaN | S |
827 | 828 | 1 | 2 | Mallet, Master. Andre | male | 1.00 | 0 | 2 | S.C./PARIS 2079 | 37.0042 | NaN | C |
7 | 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2.00 | 3 | 1 | 349909 | 21.0750 | NaN | S |
16 | 17 | 0 | 3 | Rice, Master. Eugene | male | 2.00 | 4 | 1 | 382652 | 29.1250 | NaN | Q |
119 | 120 | 0 | 3 | Andersson, Miss. Ellis Anna Maria | female | 2.00 | 4 | 2 | 347082 | 31.2750 | NaN | S |
205 | 206 | 0 | 3 | Strom, Miss. Telma Matilda | female | 2.00 | 0 | 1 | 347054 | 10.4625 | G6 | S |
297 | 298 | 0 | 1 | Allison, Miss. Helen Loraine | female | 2.00 | 1 | 2 | 113781 | 151.5500 | C22 C26 | S |
642 | 643 | 0 | 3 | Skoog, Miss. Margit Elizabeth | female | 2.00 | 3 | 2 | 347088 | 27.9000 | NaN | S |
"""年龄跟存活率有一定的关系"""
3.3 计算两个DataFrame数据相加结果
#创建两个DataFrame,test1_a test1_b
test1_a = pd.DataFrame(np.arange(9.).reshape(3, 3),
columns=['a', 'b', 'c'],
index=['one', 'two', 'three'])
test1_b = pd.DataFrame(np.arange(12.).reshape(4, 3),
columns=['a', 'e', 'c'],
index=['first', 'one', 'two', 'second'])
test1_a
a | b | c | |
---|---|---|---|
one | 0.0 | 1.0 | 2.0 |
two | 3.0 | 4.0 | 5.0 |
three | 6.0 | 7.0 | 8.0 |
test1_b
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 |
将test1_a和test1_b进行相加
test1_a+test1_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。
3.4 用describe()函数查看数据信息
describe()函数输出信息的基本含义
'''
count : 样本数据大小
mean : 样本数据的平均值
std : 样本数据的标准差
min : 样本数据的最小值
25% : 样本数据25%的时候的值
50% : 样本数据50%的时候的值
75% : 样本数据75%的时候的值
max : 样本数据的最大值
'''
data.drop(['乘客ID','乘客姓名','船票信息','客舱'],axis=1).describe()
是否幸存 | 乘客等级 | 年龄 | 兄弟姐妹个数 | 父母子女个数 | 票价 | |
---|---|---|---|---|---|---|
count | 891.000000 | 891.000000 | 714.000000 | 891.000000 | 891.000000 | 891.000000 |
mean | 0.383838 | 2.308642 | 29.699118 | 0.523008 | 0.381594 | 32.204208 |
std | 0.486592 | 0.836071 | 14.526497 | 1.102743 | 0.806057 | 49.693429 |
min | 0.000000 | 1.000000 | 0.420000 | 0.000000 | 0.000000 | 0.000000 |
25% | 0.000000 | 2.000000 | 20.125000 | 0.000000 | 0.000000 | 7.910400 |
50% | 0.000000 | 3.000000 | 28.000000 | 0.000000 | 0.000000 | 14.454200 |
75% | 1.000000 | 3.000000 | 38.000000 | 1.000000 | 0.000000 | 31.000000 |
max | 1.000000 | 3.000000 | 80.000000 | 8.000000 | 6.000000 | 512.329200 |
分析
从年龄和票价的来看,年龄最大的为80岁 票价最高512.3292元
从父母子女个数和兄弟姐妹个数数据来看,大部分人是独自旅行
幸存的人数少,大部分使用的是三等舱且年龄在三十岁上下
本文主要学习内容来源:datawhale