Pandas 基础
文件的读取和写入
文件读取
pandas可以读取的文件格式有很多,主要介绍csv,excel,txt
import numpy as np
import pandas as pd
pd.__version__
'1.1.5'
df_csv = pd.read_csv('.\data\my_csv.csv', encoding='UTF-8')
df_csv
|
col1 |
col2 |
col3 |
col4 |
col5 |
0 |
2 |
a |
1.4 |
apple |
2020/1/1 |
1 |
3 |
b |
3.4 |
banana |
2020/1/2 |
2 |
6 |
c |
2.5 |
orange |
2020/1/5 |
3 |
5 |
d |
3.2 |
lemon |
2020/1/7 |
df_txt = pd.read_table('.\data\my_table.txt', encoding= 'UTF-8')
df_txt
|
col1 |
col2 |
col3 |
col4 |
0 |
2 |
a |
1.4 |
apple 2020/1/1 |
1 |
3 |
b |
3.4 |
banana 2020/1/2 |
2 |
6 |
c |
2.5 |
orange 2020/1/5 |
3 |
5 |
d |
3.2 |
lemon 2020/1/7 |
df_excel = pd.read_excel('.\data\my_excel.xlsx')
df_excel
|
col1 |
col2 |
col3 |
col4 |
col5 |
0 |
2 |
a |
1.4 |
apple |
2020/1/1 |
1 |
3 |
b |
3.4 |
banana |
2020/1/2 |
2 |
6 |
c |
2.5 |
orange |
2020/1/5 |
3 |
5 |
d |
3.2 |
lemon |
2020/1/7 |
这里有一些常用的公共参数,header=None 表示第一行不作为列名,index_col 表示把某一列或几列作为索
引,索引的内容将会在第三章进行详述,usecols 表示读取列的集合,默认读取所有的列,parse_dates 表示
需要转化为时间的列,关于时间序列的有关内容将在第十章讲解,nrows 表示读取的数据行数。上面这些参
数在上述的三个函数里都可以使用。
pd.read_table('.\data\my_table.txt', header = None)
|
0 |
1 |
2 |
3 |
0 |
col1 |
col2 |
col3 |
col4 |
1 |
2 |
a |
1.4 |
apple 2020/1/1 |
2 |
3 |
b |
3.4 |
banana 2020/1/2 |
3 |
6 |
c |
2.5 |
orange 2020/1/5 |
4 |
5 |
d |
3.2 |
lemon 2020/1/7 |
pd.read_csv('.\data\my_csv.csv', encoding='UTF-8', index_col=['col1', 'col2'])
|
|
col3 |
col4 |
col5 |
col1 |
col2 |
|
|
|
2 |
a |
1.4 |
apple |
2020/1/1 |
3 |
b |
3.4 |
banana |
2020/1/2 |
6 |
c |
2.5 |
orange |
2020/1/5 |
5 |
d |
3.2 |
lemon |
2020/1/7 |
pd.read_table('.\data\my_table.txt', usecols=['col1', 'col2'])
|
col1 |
col2 |
0 |
2 |
a |
1 |
3 |
b |
2 |
6 |
c |
3 |
5 |
d |
pd.read_table('.\data\my_table.txt', parse_dates =['col4'])
|
col1 |
col2 |
col3 |
col4 |
0 |
2 |
a |
1.4 |
apple 2020/1/1 |
1 |
3 |
b |
3.4 |
banana 2020/1/2 |
2 |
6 |
c |
2.5 |
orange 2020/1/5 |
3 |
5 |
d |
3.2 |
lemon 2020/1/7 |
pd.read_csv('data/my_csv.csv', parse_dates=['col5'])
|
col1 |
col2 |
col3 |
col4 |
col5 |
0 |
2 |
a |
1.4 |
apple |
2020-01-01 |
1 |
3 |
b |
3.4 |
banana |
2020-01-02 |
2 |
6 |
c |
2.5 |
orange |
2020-01-05 |
3 |
5 |
d |
3.2 |
lemon |
2020-01-07 |
pd.read_excel('.\data\my_excel.xlsx', nrows = 2)
|
col1 |
col2 |
col3 |
col4 |
col5 |
0 |
2 |
a |
1.4 |
apple |
2020/1/1 |
1 |
3 |
b |
3.4 |
banana |
2020/1/2 |
在读取txt 文件时,经常遇到分隔符非空格的情况,read_table 有一个分割参数sep ,它使得用户可以自定
义分割符号,进行txt 数据的读取。例如,下面的读取的表以|||| 为分割:
pd.read_table('data/my_table_special_sep.txt')
|
col1 |||| col2 |
0 |
TS |||| This is an apple. |
1 |
GQ |||| My name is Bob. |
2 |
WT |||| Well done! |
3 |
PT |||| May I help you? |
pd.read_table('data/my_table_special_sep.txt', sep = '\|\|\|\|', engine='python')
|
col1 |
col2 |
0 |
TS |
This is an apple. |
1 |
GQ |
My name is Bob. |
2 |
WT |
Well done! |
3 |
PT |
May I help you? |
数据写入
一般在数据写入中,最常用的操作是把index 设置为False ,特别当索引没有特殊意义的时候,这样的行为
能把索引在保存的时候去除。
df_csv.to_csv('data/my_csv_saved.csv', index=False)
df_excel.to_excel('data/my_excel_saved.xlsx', index=False)
df_txt.to_csv('data/my_txt_saved.txt', sep='\t', index=False)
print(df_csv.to_markdown())
| | col1 | col2 | col3 | col4 | col5 |
|---:|-------:|:-------|-------:|:-------|:---------|
| 0 | 2 | a | 1.4 | apple | 2020/1/1 |
| 1 | 3 | b | 3.4 | banana | 2020/1/2 |
| 2 | 6 | c | 2.5 | orange | 2020/1/5 |
| 3 | 5 | d | 3.2 | lemon | 2020/1/7 |
print(df_csv.to_latex())
\begin{tabular}{lrlrll}
\toprule
{} & col1 & col2 & col3 & col4 & col5 \\
\midrule
0 & 2 & a & 1.4 & apple & 2020/1/1 \\
1 & 3 & b & 3.4 & banana & 2020/1/2 \\
2 & 6 & c & 2.5 & orange & 2020/1/5 \\
3 & 5 & d & 3.2 & lemon & 2020/1/7 \\
\bottomrule
\end{tabular}
基本数据结构
pandas 中具有两种基本的数据存储结构,存储一维values 的Series 和存储二维values 的DataFrame ,在
这两种结构上定义了很多的属性和方法。
Series
Series 一般由四个部分组成,分别是序列的值data 、索引index 、存储类型dtype 、序列的名字name 。其
中,索引也可以指定它的名字,默认为空。
s = pd.Series(data = [100, 'a', {
'dic1':5}],
index = pd.Index(['id1', 20,'third'], name = 'my_idx'),
dtype='object',
name = 'my_name')
s
my_idx
id1 100
20 a
third {'dic1': 5}
Name: my_name, dtype: object
object 类型
object 代表了一种混合类型,正如上面的例子中存储了整数、字符串以及Python 的字典数据结
构。此外,目前pandas 把纯字符串序列也默认认为是一种object 类型的序列,但它也可以用
string 类型存储,文本序列的内容会在第八章中讨论。
s.values
array([100, 'a', {'dic1': 5}], dtype=object)
s.index
Index(['id1', 20, 'third'], dtype='object', name='my_idx')
s.dtype
dtype('O')
s.name
'my_name'
s.shape
(3,)
s['third']
{'dic1': 5}
DataFrame
DataFrame 在Series 的基础上增加了列索引,一个数据框可以由二维的data 与行列索引来构造:
data =