开始之前,导入numpy、pandas包和数据
import numpy as np
import pandas as pd
df = pd.read_csv('./data/train-left-up.csv')
df.head()
| PassengerId | Survived | Pclass | Name |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... |
---|
2 | 3 | 1 | 3 | Heikkinen, Miss. Laina |
---|
3 | 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) |
---|
4 | 5 | 0 | 3 | Allen, Mr. William Henry |
---|
第二章:数据重构
2.4 数据的合并
2.4.1 任务一:将data文件夹里面的所有数据都载入,观察数据的之间的关系
text_left_up = pd.read_csv("./data/train-left-up.csv")
text_left_down = pd.read_csv("./data/train-left-down.csv")
text_right_up = pd.read_csv("./data/train-right-up.csv")
text_right_down = pd.read_csv("./data/train-right-down.csv")
text_left_up.head(2)
| PassengerId | Survived | Pclass | Name |
---|
0 | 1 | 0 | 3 | Braund, Mr. Owen Harris |
---|
1 | 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Th... |
---|
text_left_down.head(2)
| PassengerId | Survived | Pclass | Name |
---|
0 | 440 | 0 | 2 | Kvillner, Mr. Johan Henrik Johannesson |
---|
1 | 441 | 1 | 2 | Hart, Mrs. Benjamin (Esther Ada Bloomfield) |
---|
text_right_up.head(2)
| Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | male | 22.0 | 1 | 0 | A/5 21171 | 7.2500 | NaN | S |
---|
1 | female | 38.0 | 1 | 0 | PC 17599 | 71.2833 | C85 | C |
---|
text_right_down.head(2)
| Sex | Age | SibSp | Parch | Ticket | Fare | Cabin | Embarked |
---|
0 | male | 31.0 | 0 | 0 | C.A. 18723 | 10.50 | NaN | S |
---|
1 | female | 45.0 | 1 | 1 | F.C.C. 13529 | 26.25 | NaN | S |
---|
2.4.2:任务二:将数据train-left-up.csv和train-right-up.csv横向合并为一张表,并保存这张表为result_up
方法一、使用concat方法:
list_up1 = [text_left_up,text_right_up]
result_up1 = pd.concat(list_up1,axis=1)
result_up1.head(2)
| 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 |
---|
方法二、用DataFrame自带的方法join方法
result_up2 = text_left_up.join(text_right_up)
result_up2.head(2)
| 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 |
---|
方法三、使用Panads的merge方法
result_up3 = pd.merge(text_left_up,text_right_up,left_index=True,right_index=True)
result_up3.head(2)
| 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.4.3 任务三:将train-left-down和train-right-down横向合并为一张表,并保存这张表为result_down。然后将上边的result_up和result_down纵向合并为result。
list_down = [text_left_down,text_right_down]
result_down = pd.concat(list_down1,axis=1)
方法一、使用concat方法
list = [result_up1,result_down1]
result1 = pd.concat(list,axis=0)
result1.head(2)
| 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 |
---|
方法二、用DataFrame自带的方法append
result2 = result_up1.append(result_down)
result2.head(2)
| 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.4.6 任务六:完成的数据保存为result.csv
result2.to_csv("result.csv")
2.5 换一种角度看数据
2.5.1 任务一:将我们的数据变为Series类型的数据
text = pd.read_csv('result.csv')
text.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 |
---|
unit_result=text.stack().head(20)
unit_result
0 Unnamed: 0 0
PassengerId 1
Survived 0
Pclass 3
Name Braund, Mr. Owen Harris
Sex male
Age 22
SibSp 1
Parch 0
Ticket A/5 21171
Fare 7.25
Embarked S
1 Unnamed: 0 1
PassengerId 2
Survived 1
Pclass 1
Name Cumings, Mrs. John Bradley (Florence Briggs Th...
Sex female
Age 38
SibSp 1
dtype: object
unit_result.to_csv('unit_result.csv')
test = pd.read_csv('unit_result.csv')
test
| 0 | Unnamed: 0 | 0.1 |
---|
0 | 0 | PassengerId | 1 |
---|
1 | 0 | Survived | 0 |
---|
2 | 0 | Pclass | 3 |
---|
3 | 0 | Name | Braund, Mr. Owen Harris |
---|
4 | 0 | Sex | male |
---|
5 | 0 | Age | 22.0 |
---|
6 | 0 | SibSp | 1 |
---|
7 | 0 | Parch | 0 |
---|
8 | 0 | Ticket | A/5 21171 |
---|
9 | 0 | Fare | 7.25 |
---|
10 | 0 | Embarked | S |
---|
11 | 1 | Unnamed: 0 | 1 |
---|
12 | 1 | PassengerId | 2 |
---|
13 | 1 | Survived | 1 |
---|
14 | 1 | Pclass | 1 |
---|
15 | 1 | Name | Cumings, Mrs. John Bradley (Florence Briggs Th... |
---|
16 | 1 | Sex | female |
---|
17 | 1 | Age | 38.0 |
---|
18 | 1 | SibSp | 1 |
---|
|