直接看程序吧:
# 通過ts讀取數據到df,存入csv文件,再讀出來
import pandas as pd
import tushare as ts
from pandas import DataFrame
# 讀取ts數據
df = ts.get_k_data('sh600519', '1980-01-01') # 當然,它是2001年上市的
print('df:\n', df)
# 寫入csv
df.to_csv('sh600519')
# 原汁原味讀出
df1 = pd.read_csv('sh600519')
# 用date作索引,並把它由字符串轉爲date對象
df2 = pd.read_csv('sh600519', index_col = 'date',
parse_dates=['date'])
# 在df2的基礎上,只讀取指定的列
df3 = pd.read_csv('sh600519', index_col = 'date',
parse_dates=['date'])[['open','close','high','low','volume','code']]
# 看一看讀的效果
print('df1:\n', df1)
print('df2:\n', df2)
print('df3:\n', df3)
運行結果:
df:
date open close high low volume code
0 2001-08-27 5.392 5.554 5.902 5.132 406318.00 sh600519
1 2001-08-28 5.467 5.759 5.781 5.407 129647.79 sh600519
2 2001-08-29 5.777 5.684 5.781 5.640 53252.75 sh600519
3 2001-08-30 5.668 5.796 5.860 5.624 48013.06 sh600519
4 2001-08-31 5.804 5.782 5.877 5.749 23231.48 sh600519
... ... ... ... ... ... ... ...
4452 2020-04-24 1248.000 1250.560 1259.890 1235.180 19122.00 sh600519
4453 2020-04-27 1257.000 1276.000 1278.170 1250.960 25904.00 sh600519
4454 2020-04-28 1285.310 1279.130 1299.940 1271.880 34662.00 sh600519
4455 2020-04-29 1277.800 1274.900 1288.100 1258.000 23444.00 sh600519
4456 2020-04-30 1271.000 1265.700 1285.010 1258.880 24661.00 sh600519
[4457 rows x 7 columns]
df1:
Unnamed: 0 date open ... low volume code
0 0 2001-08-27 5.392 ... 5.132 406318.00 sh600519
1 1 2001-08-28 5.467 ... 5.407 129647.79 sh600519
2 2 2001-08-29 5.777 ... 5.640 53252.75 sh600519
3 3 2001-08-30 5.668 ... 5.624 48013.06 sh600519
4 4 2001-08-31 5.804 ... 5.749 23231.48 sh600519
... ... ... ... ... ... ... ...
4452 4452 2020-04-24 1248.000 ... 1235.180 19122.00 sh600519
4453 4453 2020-04-27 1257.000 ... 1250.960 25904.00 sh600519
4454 4454 2020-04-28 1285.310 ... 1271.880 34662.00 sh600519
4455 4455 2020-04-29 1277.800 ... 1258.000 23444.00 sh600519
4456 4456 2020-04-30 1271.000 ... 1258.880 24661.00 sh600519
[4457 rows x 8 columns]
df2:
Unnamed: 0 open close ... low volume code
date ...
2001-08-27 0 5.392 5.554 ... 5.132 406318.00 sh600519
2001-08-28 1 5.467 5.759 ... 5.407 129647.79 sh600519
2001-08-29 2 5.777 5.684 ... 5.640 53252.75 sh600519
2001-08-30 3 5.668 5.796 ... 5.624 48013.06 sh600519
2001-08-31 4 5.804 5.782 ... 5.749 23231.48 sh600519
... ... ... ... ... ... ... ...
2020-04-24 4452 1248.000 1250.560 ... 1235.180 19122.00 sh600519
2020-04-27 4453 1257.000 1276.000 ... 1250.960 25904.00 sh600519
2020-04-28 4454 1285.310 1279.130 ... 1271.880 34662.00 sh600519
2020-04-29 4455 1277.800 1274.900 ... 1258.000 23444.00 sh600519
2020-04-30 4456 1271.000 1265.700 ... 1258.880 24661.00 sh600519
[4457 rows x 7 columns]
df3:
open close high low volume code
date
2001-08-27 5.392 5.554 5.902 5.132 406318.00 sh600519
2001-08-28 5.467 5.759 5.781 5.407 129647.79 sh600519
2001-08-29 5.777 5.684 5.781 5.640 53252.75 sh600519
2001-08-30 5.668 5.796 5.860 5.624 48013.06 sh600519
2001-08-31 5.804 5.782 5.877 5.749 23231.48 sh600519
... ... ... ... ... ... ...
2020-04-24 1248.000 1250.560 1259.890 1235.180 19122.00 sh600519
2020-04-27 1257.000 1276.000 1278.170 1250.960 25904.00 sh600519
2020-04-28 1285.310 1279.130 1299.940 1271.880 34662.00 sh600519
2020-04-29 1277.800 1274.900 1288.100 1258.000 23444.00 sh600519
2020-04-30 1271.000 1265.700 1285.010 1258.880 24661.00 sh600519
[4457 rows x 6 columns]