1, 用法的数据库
import numpy as np import pandas as pd import matplotlib.pyplot as plt import tushare as ts
2.数据的获取
data = ts.get_hist_data('000012',start='2015-06-23',end='2017-11-16') print(data.tail())
3.某列数据的提取
# 数据的逐个提取,并运算data["ret_loop"] = 0.0 # 新增一列数值为零的数据for i in range(1,len(data)): # 循环 data["ret_loop"][i]=np.log(data['close'][i]/data['close'][i-1]) # 逐个提取数据并运算
4. 向量化运算
data['return']=np.log(data["close"]/data["close"].shift(1)) # 向量化运算 快
5.列数据的提取并绘图
# data[['return',"close"]].plot() # 两列数据的提取 # 将两列数据放到一个图里面 data[['return',"close"]].plot(subplots=True,style='b',figsize=(8,5)) # 分别显示 plt.show()
l另外的获取方法
需要安装的数据库
$ pip install pandas $ pip install pandas-datareader
代码:
import datetime import pandas_datareader.data as web start = datetime.datetime(2016, 1, 1) # or start = '1/1/2016' end = datetime.date.today() data = web.DataReader('GOOG',data_source='yahoo',start=start,end=end) print(data.tail())