Tushare财经数据接口
股票基本面统计
使用get_stock_basics()函数一次性下载所有股票基本面数据。这对观察股票的整体市场情况很有帮助。
import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
stock = ts.get_stock_basics() # 下载股票基本面数据
stock.to_excel('stock.xlsx') # 保存为电子表格
stock.shape # Out: (3678, 22)
数据集规模为3823x22,每行是一支股票的基本数据。字段详情请查看Tushare的网站。数据集字段详情查看http://tushare.org 网站。本节用到的数据列有:code,股票代码;name,名称;industry,所属行业;area,地区;pe,市盈率;totals,总股本(亿元人民币);esp,每股收益;timeToMarket,上市日期。
下面从电子表格文件中读取数据,注意股票代码code列的处理细节。Pandas读取数据时,总是试图将数据自动转换为数值类型。深市类似’002522’的股票代码读入后,将丢失前导字符“00”,变为整数2522,因此读取时特意指定code字段为字符串。
df = pd.read_excel('stock.xlsx', dtype={
'code': 'str'})