1
1
# Pandas
2
2
3
+ Pandas是Python中用于数据处理和分析的库,尤其对于大数据行业的数据清洗很有帮助。
4
+ > 通过带有标签的列和索引,Pandas 使我们可以以一种所有人都能理解的方式来处理数据。它可以让我们毫不费力地从诸如 csv 类型的文件中导入数据。我们可以用它快速地对数据进行复杂的转换和过滤等操作。
5
+
3
6
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->
4
7
5
8
- [ Pandas] ( #pandas )
6
- - [First step 开始使用Pandas](#first-step- 开始使用pandas)
7
- - [Data types 数据类型 ](#data-types-数据类型 )
8
- - [Read and write data 文件操作](#read-and-write-data- 文件操作)
9
- - [Read data 导入数据 ](#read-data-导入数据 )
10
- - [Write data 保存数据](#write-data- 保存数据)
11
- - [Data operations 数据操作](#data-operations- 数据操作)
12
- - [Column operations 列操作 ](#column-operations-列操作 )
13
- - [Row operations 行操作 ](#row-operations-行操作 )
14
- - [Cell operations 单元格操作 ](#cell-operations-单元格操作 )
15
- - [Filter data 数据筛选](#filter-data- 数据筛选)
16
- - [Index 索引 ](#index-索引 )
17
- - [Sort 排序 ](#sort-排序 )
18
- - [Applying functions to datasets 对数据集应用函数](#applying-functions-to-datasets- 对数据集应用函数)
19
- - [Manipulating a dataset's structure 操作数据集的结构](#manipulating-a-datasets-structure- 操作数据集的结构)
20
- - [Combining datasets 合并数据集](#combining-datasets- 合并数据集)
21
- - [Plot graphs quickly 快速画图](#plot-graphs-quickly- 快速画图)
22
- - [References: ](#references)
9
+ - [开始使用Pandas](#开始使用pandas)
10
+ - [数据类型 Data types](#数据类型- data-types)
11
+ - [文件操作](#文件操作)
12
+ - [从文件导入数据 ](#从文件导入数据 )
13
+ - [保存数据](#保存数据)
14
+ - [数据操作](#数据操作)
15
+ - [列操作 Column operations](#列操作- column-operations)
16
+ - [行操作 Row operations](#行操作- row-operations)
17
+ - [单元格操作 Cell operations](#单元格操作- cell-operations)
18
+ - [数据筛选](#数据筛选)
19
+ - [索引 Index ](#索引-index )
20
+ - [排序 Sort ](#排序-sort )
21
+ - [对数据集应用函数](#对数据集应用函数)
22
+ - [操作数据集的结构](#操作数据集的结构)
23
+ - [合并数据集](#合并数据集)
24
+ - [快速画图](#快速画图)
25
+ - [References](#references)
23
26
24
27
<!-- /TOC -->
25
28
26
- Pandas是Python中的一个库,主要用于清洗数据。
27
-
28
- > 通过带有标签的列和索引,Pandas 使我们可以以一种所有人都能理解的方式来处理数据。它可以让我们毫不费力地从诸如 csv 类型的文件中导入数据。我们可以用它快速地对数据进行复杂的转换和过滤等操作。
29
-
30
- ## First step 开始使用Pandas
29
+ ## 开始使用Pandas
30
+ 对于使用 _ Python_ 库,第一步必然是` import ` :
31
31
``` python
32
32
import pandas as pd
33
- df = pd.read_csv(csv_path)
34
33
```
35
34
36
- ## Data types 数据类型
35
+ ## 数据类型 Data types
37
36
Pandas 基于两种数据类型,` series ` 和 ` dataframe ` 。
38
37
* ** ` series ` ** 是一种一维的数据类型,其中的每个元素都有各自的标签。可以当作一个由带标签的元素组成的 numpy 数组。标签可以是数字或者字符。
39
38
* ** ` dataframe ` ** 是一个二维的、表格型的数据结构。` Pandas ` 的 ` dataframe ` 可以储存许多不同类型的数据,并且每个轴都有标签。你可以把它当作一个 ` series ` 的字典。
40
39
41
- ## Read and write data 文件操作
42
- ### Read data 导入数据
40
+ ## 文件操作
41
+ ### 从文件导入数据
43
42
* ` read_csv() ` :读取csv文件为` dataframe `
44
43
``` python
45
44
# Reading a csv into Pandas.
@@ -49,15 +48,15 @@ Pandas 基于两种数据类型,`series` 和 `dataframe`。
49
48
* ` df.head() ` :查看前5行数据
50
49
* ` df.tail() ` :查看前最后5行数据
51
50
52
- ### Write data 保存数据
51
+ ### 保存数据
53
52
* ` to_csv() ` :` dataframe ` 存入csv文件
54
53
``` python
55
54
# Reading a csv into Pandas.
56
55
df.to_csv(' new.csv' )
57
56
```
58
57
59
- ## Data operations 数据操作
60
- ### Column operations 列操作
58
+ ## 数据操作
59
+ ### 列操作 Column operations
61
60
* 获取一列,返回的是` series ` :
62
61
* ` df['rain_octsep'] `
63
62
* ` df.rain_octsep ` :也像访问属性一样访问列
@@ -69,15 +68,15 @@ df.columns = ['water_year','rain_octsep', 'outflow_octsep',
69
68
' rain_junaug' , ' outflow_junaug' ]
70
69
```
71
70
72
- ### Row operations 行操作
71
+ ### 行操作 Row operations
73
72
* ` len(df) ` :返回数据集的总行数
74
73
75
- ### Cell operations 单元格操作
74
+ ### 单元格操作 Cell operations
76
75
* ` df.ix[i, j] ` :返回` i ` 行` j ` 列的单元格数据,` i ` 、` j ` 可以是` index ` 或者` label `
77
76
* ` df.ix[i0:i1, j0:j1] ` :支持` slicing ` ,返回一个sub-dataframe
78
77
* ` df.['label'].unique() ` :获得唯一的值列表
79
78
80
- ### Filter data 数据筛选
79
+ ### 数据筛选
81
80
* 根据column范围筛选数据(` 布尔过滤 boolean masking ` ):
82
81
* ** 注意** :条件里不能用 and 关键字,因为会引发操作顺序的问题。必须用 & 和圆括号。
83
82
* 当使用字符串过滤时,需要用` .str.[string method] ` ,而不能直接在字符串上调用字符方法。
@@ -87,7 +86,7 @@ df[(df.rain_octsep < 1000) & (df.outflow_octsep < 4000)]
87
86
df[df.water_year.str.startswith(' 199' )] # 使用字符串过滤
88
87
```
89
88
90
- ### Index 索引
89
+ ### 索引 Index
91
90
可以根据索引来获取某一行,而且获取行数据的方法是根据标签的类型变化而变化的。
92
91
93
92
* 如果标签是数字型的,可以通过 iloc 来引用:
@@ -105,15 +104,15 @@ df[df.water_year.str.startswith('199')] # 使用字符串过滤
105
104
```
106
105
* 还有一个引用列的常用常用方法 ` ix ` 。` loc ` 是基于标签的,而 ` iloc ` 是基于数字的,而 ` ix ` 是基于标签的查询方法,但它同时也支持数字型索引作为备选。** 注意** :` ix ` 具有轻微的不可预测性,它所支持的数字型索引只是备选,可能会导致 ` ix ` 产生一些奇怪的结果,比如将一个数字解释为一个位置。而使用 ` iloc ` 和 ` loc ` 会很安全、可预测。但 ` ix ` 比 ` iloc ` 和 ` loc ` 要快一些。
107
106
108
- #### Sort 排序
107
+ #### 排序 Sort
109
108
将索引排序通常会很有用,在 Pandas 中,我们可以对 dataframe 调用 sort_index 方法进行排序。
110
109
``` python
111
110
df.sort_index(ascending = False )
112
111
```
113
112
114
113
当将一列设置为索引的时候,它就不再是数据的一部分了。如果你想将索引恢复为数据,调用` set_index ` 相反的方法 ` reset_index ` 即可:
115
114
116
- ### Applying functions to datasets 对数据集应用函数
115
+ ### 对数据集应用函数
117
116
有时你想对数据集中的数据进行改变或者某种操作。比方说,你有一列年份的数据,你需要新的一列来表示这些年份对应的年代。_ Pandas_ 中有两个非常有用的函数, ` apply ` 和 ` applymap ` 。
118
117
``` python
119
118
def base_year (year ):
@@ -125,13 +124,13 @@ df['year'] = df.water_year.apply(base_year)
125
124
df.head(5 )
126
125
```
127
126
128
- ### Manipulating a dataset's structure 操作数据集的结构
127
+ ### 操作数据集的结构
129
128
* groupby()
130
129
* max() 、 min() 、mean()
131
130
* unstack()
132
131
* pivot():旋转
133
132
134
- ### Combining datasets 合并数据集
133
+ ### 合并数据集
135
134
将有两个相关联的数据集放在一起:
136
135
``` python
137
136
rain_jpn = pd.read_csv(' jpn_rain.csv' )
@@ -142,14 +141,16 @@ uk_jpn_rain.head(5)
142
141
143
142
需要通过 on 关键字来指定需要合并的列。通常你可以省略这个参数,Pandas 将会自动选择要合并的列。
144
143
145
- ## Plot graphs quickly 快速画图
144
+ ## 快速画图
146
145
Matplotlib 很棒,但是想要绘制出还算不错的图表却要写不少代码,而有时你只是想粗略的做个图来探索下数据,搞清楚数据的含义。Pandas 通过 plot 来解决这个问题:
147
146
``` python
148
147
uk_jpn_rain.plot(x = ' year' , y = [' rain_octsep' , ' jpn_rainfall' ])
149
148
```
150
149
<p align =" center " >
151
- <img src =" http ://liubj2016.github.io/Akuan/images/tu.png" />
150
+ <img src =" https ://liubj2016.github.io/Akuan/images/tu.png" />
152
151
</p >
153
152
154
- ## References:
153
+ ## References
155
154
* [ An Introduction to Scientific Python – Pandas] ( http://www.datadependence.com/2016/05/scientific-python-pandas/ )
155
+
156
+ [ 回到目录] ( #pandas )
0 commit comments