Skip to content

Commit a718a9b

Browse files
committed
update python related notes' format
1 parent e1d101e commit a718a9b

File tree

4 files changed

+47
-41
lines changed

4 files changed

+47
-41
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@
2828
* 机器学习工具
2929
* [Google Colaboratory](https://colab.research.google.com):机器学习Jupyter环境
3030
* [IBM Cognitive Class Lab](https://labs.cognitiveclass.ai):机器学习环境
31+
32+
33+
[回到目录](#numpy)

python/numpy/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ array([[False, False, False, False],
297297
array([ 5, 6, 7, 8, 9, 10, 11])
298298
```
299299

300-
选择性负值
300+
选择性赋值
301301
``` python
302302
>>> a[b] = 0
303303
>>> a

python/pandas/README.md

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
11
# Pandas
22

3+
Pandas是Python中用于数据处理和分析的库,尤其对于大数据行业的数据清洗很有帮助。
4+
> 通过带有标签的列和索引,Pandas 使我们可以以一种所有人都能理解的方式来处理数据。它可以让我们毫不费力地从诸如 csv 类型的文件中导入数据。我们可以用它快速地对数据进行复杂的转换和过滤等操作。
5+
36
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->
47

58
- [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)
2326

2427
<!-- /TOC -->
2528

26-
Pandas是Python中的一个库,主要用于清洗数据。
27-
28-
> 通过带有标签的列和索引,Pandas 使我们可以以一种所有人都能理解的方式来处理数据。它可以让我们毫不费力地从诸如 csv 类型的文件中导入数据。我们可以用它快速地对数据进行复杂的转换和过滤等操作。
29-
30-
## First step 开始使用Pandas
29+
## 开始使用Pandas
30+
对于使用 _Python_ 库,第一步必然是`import`
3131
``` python
3232
import pandas as pd
33-
df = pd.read_csv(csv_path)
3433
```
3534

36-
## Data types 数据类型
35+
## 数据类型 Data types
3736
Pandas 基于两种数据类型,`series``dataframe`
3837
* **`series`** 是一种一维的数据类型,其中的每个元素都有各自的标签。可以当作一个由带标签的元素组成的 numpy 数组。标签可以是数字或者字符。
3938
* **`dataframe`** 是一个二维的、表格型的数据结构。`Pandas``dataframe` 可以储存许多不同类型的数据,并且每个轴都有标签。你可以把它当作一个 `series` 的字典。
4039

41-
## Read and write data 文件操作
42-
### Read data 导入数据
40+
## 文件操作
41+
### 从文件导入数据
4342
* `read_csv()`:读取csv文件为`dataframe`
4443
``` python
4544
# Reading a csv into Pandas.
@@ -49,15 +48,15 @@ Pandas 基于两种数据类型,`series` 和 `dataframe`。
4948
* `df.head()`:查看前5行数据
5049
* `df.tail()`:查看前最后5行数据
5150

52-
### Write data 保存数据
51+
### 保存数据
5352
* `to_csv()``dataframe`存入csv文件
5453
``` python
5554
# Reading a csv into Pandas.
5655
df.to_csv('new.csv')
5756
```
5857

59-
## Data operations 数据操作
60-
### Column operations 列操作
58+
## 数据操作
59+
### 列操作 Column operations
6160
* 获取一列,返回的是`series`
6261
* `df['rain_octsep']`
6362
* `df.rain_octsep`:也像访问属性一样访问列
@@ -69,15 +68,15 @@ df.columns = ['water_year','rain_octsep', 'outflow_octsep',
6968
'rain_junaug', 'outflow_junaug']
7069
```
7170

72-
### Row operations 行操作
71+
### 行操作 Row operations
7372
* `len(df)`:返回数据集的总行数
7473

75-
### Cell operations 单元格操作
74+
### 单元格操作 Cell operations
7675
* `df.ix[i, j]`:返回`i``j`列的单元格数据,`i``j`可以是`index`或者`label`
7776
* `df.ix[i0:i1, j0:j1]`:支持`slicing`,返回一个sub-dataframe
7877
* `df.['label'].unique()`:获得唯一的值列表
7978

80-
### Filter data 数据筛选
79+
### 数据筛选
8180
* 根据column范围筛选数据(`布尔过滤 boolean masking`):
8281
* **注意**:条件里不能用 and 关键字,因为会引发操作顺序的问题。必须用 & 和圆括号。
8382
* 当使用字符串过滤时,需要用`.str.[string method]`,而不能直接在字符串上调用字符方法。
@@ -87,7 +86,7 @@ df[(df.rain_octsep < 1000) & (df.outflow_octsep < 4000)]
8786
df[df.water_year.str.startswith('199')] # 使用字符串过滤
8887
```
8988

90-
### Index 索引
89+
### 索引 Index
9190
可以根据索引来获取某一行,而且获取行数据的方法是根据标签的类型变化而变化的。
9291

9392
* 如果标签是数字型的,可以通过 iloc 来引用:
@@ -105,15 +104,15 @@ df[df.water_year.str.startswith('199')] # 使用字符串过滤
105104
```
106105
* 还有一个引用列的常用常用方法 `ix``loc` 是基于标签的,而 `iloc` 是基于数字的,而 `ix` 是基于标签的查询方法,但它同时也支持数字型索引作为备选。**注意**`ix` 具有轻微的不可预测性,它所支持的数字型索引只是备选,可能会导致 `ix` 产生一些奇怪的结果,比如将一个数字解释为一个位置。而使用 `iloc``loc` 会很安全、可预测。但 `ix``iloc``loc` 要快一些。
107106

108-
#### Sort 排序
107+
#### 排序 Sort
109108
将索引排序通常会很有用,在 Pandas 中,我们可以对 dataframe 调用 sort_index 方法进行排序。
110109
``` python
111110
df.sort_index(ascending=False)
112111
```
113112

114113
当将一列设置为索引的时候,它就不再是数据的一部分了。如果你想将索引恢复为数据,调用`set_index` 相反的方法 `reset_index` 即可:
115114

116-
### Applying functions to datasets 对数据集应用函数
115+
### 对数据集应用函数
117116
有时你想对数据集中的数据进行改变或者某种操作。比方说,你有一列年份的数据,你需要新的一列来表示这些年份对应的年代。_Pandas_ 中有两个非常有用的函数, `apply``applymap`
118117
``` python
119118
def base_year(year):
@@ -125,13 +124,13 @@ df['year'] = df.water_year.apply(base_year)
125124
df.head(5)
126125
```
127126

128-
### Manipulating a dataset's structure 操作数据集的结构
127+
### 操作数据集的结构
129128
* groupby()
130129
* max() 、 min() 、mean()
131130
* unstack()
132131
* pivot():旋转
133132

134-
### Combining datasets 合并数据集
133+
### 合并数据集
135134
将有两个相关联的数据集放在一起:
136135
``` python
137136
rain_jpn = pd.read_csv('jpn_rain.csv')
@@ -142,14 +141,16 @@ uk_jpn_rain.head(5)
142141

143142
需要通过 on 关键字来指定需要合并的列。通常你可以省略这个参数,Pandas 将会自动选择要合并的列。
144143

145-
## Plot graphs quickly 快速画图
144+
## 快速画图
146145
Matplotlib 很棒,但是想要绘制出还算不错的图表却要写不少代码,而有时你只是想粗略的做个图来探索下数据,搞清楚数据的含义。Pandas 通过 plot 来解决这个问题:
147146
``` python
148147
uk_jpn_rain.plot(x='year', y=['rain_octsep', 'jpn_rainfall'])
149148
```
150149
<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" />
152151
</p>
153152

154-
## References:
153+
## References
155154
* [An Introduction to Scientific Python – Pandas](http://www.datadependence.com/2016/05/scientific-python-pandas/)
155+
156+
[回到目录](#pandas)

python/python-basic/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,5 @@ with open("example.txt", "w") as file:
331331
### Delete a File or Folder 删除文件或目录
332332
* 删除一个文件或目录:
333333
* `os.remove("filename_or_foldername")`
334+
335+
[回到目录](#python基础)

0 commit comments

Comments
 (0)