Pandas文件读取

简介: Pandas文件读取

1. 文件读取


pandas可以读取的文件格式有很多,这里主要介绍读取csv, excel, txt文件。

df_csv = pd.read_csv('../data/my_csv.csv')
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')
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', 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_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?   |

上面的结果显然不是理想的,这时可以使用sep,同时需要指定引擎为python

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?


【WARNING】sep是正则参数


在使用read_table的时候需要注意,参数sep中使用的是正则表达式,因此需要对|进行转义变成|,否则无法读取到正确的结果。有关正则表达式的基本内容可以参考第八章或者其他相关资料。


【END】


2. 数据写入


一般在数据写入中,最常用的操作是把index设置为False,特别当索引没有特殊意义的时候,这样的行为能把索引在保存的时候去除。

df_csv.to_csv('../data/my_csv_saved.csv', index=False)
df_excel.to_excel('../data/my_excel_saved.xlsx', index=False)

pandas中没有定义to_table函数,但是to_csv可以保存为txt文件,并且允许自定义分隔符,常用制表符\t分割:

df_txt.to_csv('../data/my_txt_saved.txt', sep='\t', index=False)

如果想要把表格快速转换为markdownlatex语言,可以使用to_markdownto_latex函数,此处需要安装tabulate包。

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}


目录
相关文章
|
1天前
|
数据采集 索引 Python
|
18天前
|
数据采集 数据挖掘 数据处理
使用Python和Pandas处理CSV数据
使用Python和Pandas处理CSV数据
68 5
|
4月前
|
数据挖掘 索引 Python
Python 教程之 Pandas(15)—— 使用 pandas.read_csv() 读取 csv
Python 教程之 Pandas(15)—— 使用 pandas.read_csv() 读取 csv
61 0
|
10月前
|
存储 数据处理 索引
Pandas读取Excel文件内容的方法使用正确的指南
Pandas读取Excel文件内容的方法使用正确的指南
|
存储 JSON 数据挖掘
pandas玩转excel_pandas操作excel_pandas读写excel
pandas玩转excel_pandas操作excel_pandas读写excel
419 0
|
数据采集 存储 JSON
Pandas 加载数据的方法和技巧
Pandas 加载数据的方法和技巧
|
存储 SQL JSON
Python 之 Pandas 文件操作和读取 CSV 参数详解
Python 之 Pandas 文件操作和读取 CSV 参数详解
|
Python
Python库函数pandas读取excel数据
pandas 读取excel文件使用的是 read_excel方法。本文将详细解析read_excel方法的常用参数,以及实际的使用示例
|
Python
Python库函数pandas读取csv文件
pandas 读取csv文件使用的是 read_csv方法。本文将详细解析read_csv方法的常用参数,以及实际的使用示例
|
数据采集 前端开发 数据可视化
20 个短小精悍的 pandas 骚操作
本次为大家整理了一个pandas骚操作操作的大集合,共20个功能,个个短小精悍,一次让你爱个够。系列内容,请看👉「pandas100个骚操作」话题。
20 个短小精悍的 pandas 骚操作