import pandas as pd
- 我们的数据大部分存在于文件当中,所以pandas会支持复杂的IO操作
- pandas的API支持众多的文件格式,例如CSV,SQL,XLS,JSON,HDF5
CSV文件
read_csv 读取文件
- pandas.read_csv(filepath_or_buffer,sep =’,’,usecols)
- filepath_or_buffer:文件路径
- sep:分隔符,默认用“,”替换
- usecols:指定重新读取的列名,列表形式
# 读取文件,并且指定只获取'open', 'close'指标
data = pd.read_csv("./data/stock_day.csv",usecols=['open', 'close'])
# 读取文件,并且获取所有
# data = pd.read_csv("./data/stock_day.csv")
data.head()
open | close | |
---|---|---|
2018-02-27 | 23.53 | 24.16 |
2018-02-26 | 22.80 | 23.53 |
2018-02-23 | 22.88 | 22.82 |
2018-02-22 | 22.25 | 22.28 |
2018-02-14 | 21.49 | 21.92 |
to_csv 保存文件
-
DataFrame.to_csv(path_or_buf =无,sep =’,’,columns=无,header=真,index=真,模式=‘w’,编码=无)
-
path_or_buf:文件路径
-
sep:分隔符,默认用“,”替换
-
columns:选择需要的列索引
-
header:布尔值或字符串列表,默认为True,是否写进列索引值
-
index:是否写进行索引
-
模式:‘w’:重组,'a’追加
# 保存'open'列的数据,然后读取查看结果
# 选取前10行数据保存,便于观察数据
data[:10].to_csv("./data/test1.csv",columns=['open'])
# 读取,查看结果
pd.read_csv("./data/test1.csv")
Unnamed: 0 | open | |
---|---|---|
0 | 2018-02-27 | 23.53 |
1 | 2018-02-26 | 22.80 |
2 | 2018-02-23 | 22.88 |
3 | 2018-02-22 | 22.25 |
4 | 2018-02-14 | 21.49 |
5 | 2018-02-13 | 21.40 |
6 | 2018-02-12 | 20.70 |
7 | 2018-02-09 | 21.20 |
8 | 2018-02-08 | 21.79 |
9 | 2018-02-07 | 22.69 |
# 会发现将索引存入到文件当中,变成单独的一列数据。
# 如果需要删除,可以指定索引参数,删除原来的文件,重新保存一次。
data[:10].to_csv("./data/test2.csv",columns=['open'],index=False)
pd.read_csv("./data/test2.csv").head()
open | |
---|---|
0 | 23.53 |
1 | 22.80 |
2 | 22.88 |
3 | 22.25 |
4 | 21.49 |
HDF5
read_hdf
-
HDF5文件的读取和存储需要指定一个键,估计要存储的DataFrame
-
pandas.read_hdf(path_or_buf,key = None,** kwargs)
从h5文件当中读取数据
-
path_or_buffer:文件路径
-
key:读取的键
-
返回:所选对象
# 读取
day_close = pd.read_hdf('./data/day_close.h5')
day_close.head()
000001.SZ | 000002.SZ | 000004.SZ | 000005.SZ | 000006.SZ | 000007.SZ | 000008.SZ | 000009.SZ | 000010.SZ | 000011.SZ | ... | 001965.SZ | 603283.SH | 002920.SZ | 002921.SZ | 300684.SZ | 002922.SZ | 300735.SZ | 603329.SH | 603655.SH | 603080.SH | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 16.30 | 17.71 | 4.58 | 2.88 | 14.60 | 2.62 | 4.96 | 4.66 | 5.37 | 6.02 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | 17.02 | 19.20 | 4.65 | 3.02 | 15.97 | 2.65 | 4.95 | 4.70 | 5.37 | 6.27 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | 17.02 | 17.28 | 4.56 | 3.06 | 14.37 | 2.63 | 4.82 | 4.47 | 5.37 | 5.96 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
3 | 16.18 | 16.97 | 4.49 | 2.95 | 13.10 | 2.73 | 4.89 | 4.33 | 5.37 | 5.77 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
4 | 16.95 | 17.19 | 4.55 | 2.99 | 13.18 | 2.77 | 4.97 | 4.42 | 5.37 | 5.92 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
5 rows × 3562 columns
to_hdf
- HDF5文件的读取和存储需要指定一个键,估计要存储的DataFrame
- DataFrame.to_hdf(path_or_buf,key,* \ kwargs *)
# 存储文件,.h5文件需要指定键
day_close.to_hdf("./data/test1.h5", key="day1")
# 读取存储的.h5文件,再次读取的时候,需要指定键的名字
new_close = pd.read_hdf('./data/test1.h5', key='day1')
new_close.head()
000001.SZ | 000002.SZ | 000004.SZ | 000005.SZ | 000006.SZ | 000007.SZ | 000008.SZ | 000009.SZ | 000010.SZ | 000011.SZ | ... | 001965.SZ | 603283.SH | 002920.SZ | 002921.SZ | 300684.SZ | 002922.SZ | 300735.SZ | 603329.SH | 603655.SH | 603080.SH | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 16.30 | 17.71 | 4.58 | 2.88 | 14.60 | 2.62 | 4.96 | 4.66 | 5.37 | 6.02 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
1 | 17.02 | 19.20 | 4.65 | 3.02 | 15.97 | 2.65 | 4.95 | 4.70 | 5.37 | 6.27 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
2 | 17.02 | 17.28 | 4.56 | 3.06 | 14.37 | 2.63 | 4.82 | 4.47 | 5.37 | 5.96 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
3 | 16.18 | 16.97 | 4.49 | 2.95 | 13.10 | 2.73 | 4.89 | 4.33 | 5.37 | 5.77 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
4 | 16.95 | 17.19 | 4.55 | 2.99 | 13.18 | 2.77 | 4.97 | 4.42 | 5.37 | 5.92 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
5 rows × 3562 columns
注意:优先选择使用HDF5文件存储
- HDF5在存储的时候支持压缩,使用的方式是blosc,这个是速度驱动的也是pandas 替代支持的
- 使用压缩可以提磁盘优化,节省空间
- HDF5还是跨平台的,可以轻松迁移到hadoop上面
JSON
- JSON是我们常用的一种数据交换格式,在前面的交互经常用到,也会在存储的时候选择这种格式。
- 所以我们需要知道Pandas如何进行读取和存储JSON格式。
read_JSON
- pandas.read_json(path_or_buf = None,orient = None,typ =‘frame’,lines = False)