前言
本文将我国A股上市公司作为研究对象,选取了A股 2015-2019 年度被 ST 或被 *ST上市公司,剔除了部分非财务原因导致ST或*ST的上市公司。财务指标选择了T-3期的资产负债率、流动比率、应收账款周转率等10个财务指标。
数据收集及预处理
导入所需要库
import tushare as tspro = ts.pro_api('*') #tushre的tokenimport pandas as pdimport time
通过tushare.pro取得上市公司信息列表
data_Lcompany=pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol')data_Dcompany=pro.stock_basic(exchange='', list_status='D', fields='ts_code,symbol')data_Pcompany=pro.stock_basic(exchange='', list_status='P', fields='ts_code,symbol')data_company=pd.concat([data_Lcompany,data_Dcompany,data_Pcompany]
读取CSMAR数据库导出的样本数据并做数据预处理
data_sample=pd.read_excel(r"C:\Users\hasee\Desktop\样本.xlsx")data_sample.columns=data_sample.iloc[0]data_sample.drop(index=0,inplace=True)data_sample.loc[:,"执行日期"]=pd.to_datetime(data_sample.loc[:,"执行日期"])data_sample["年份"]=pd.DatetimeIndex(data_sample.loc[:,"执行日期"]).yeardata_sample=data_sample[["证券代码","年份"]]data_sample.columns=["symbol","年份"]data_sample=pd.merge(data_sample,data_company,how='left',on="symbol"
对样本数据进一步预处理,与正常公司数据合并,通过tushare.pro读取财务指标并写入csv
year=[2015,2016,2017,2018,2019]createVar = locals()for j in year: createVar["data_sample_"+str(j)]=data_sample[data_sample["年份"]==j] createVar["data_origin_"+str(j)]=pd.merge(data_Lcompany,createVar["data_sample_"+str(j)],how='outer') createVar["data_origin_"+str(j)]["label"]=createVar["data_origin_"+str(j)]["年份"].map({j:0}) createVar["data_origin_"+str(j)].fillna(1,inplace=True) print(j) del createVar["data_origin_"+str(j)]["年份"] for i in range(createVar["data_origin_"+str(j)].shape[0]): print(i) if i==0: createVar["fina_indicator_"+str(j)]=pro.query('fina_indicator', ts_code=createVar["data_origin_"