akshare获取上证指数,板块指数,以时间序列为索引并合并。tushare获取个股信息。
二组数据重索引合并
df3.index = range(df3.shape[0])
df.index = range(df.shape[0])
Tf=pd.concat([df,df3],ignore_index = True,axis=1)
import pandas as pd
import akshare as ak
import tushare as ts
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
scokt= '600096'
start = '2018-01-02'
end = '2022-05-06'
startyear = '2018'
symbols = "磷化工"
df1 = ak.stock_zh_index_daily(symbol="sh000001")
df1=df1.iloc[6613: ,:]
df2 = ak.stock_board_concept_hist_ths(start_year = startyear ,symbol = symbols)
df1.sort_values(by='date',inplace=True)
df2.sort_values(by='日期',inplace=True)
df1.index=df1['date']
df2.index=df2['日期']
df3=pd.concat([df1,df2],ignore_index = True,axis=1)
####个股获取
# 数据获取
def get_from_tushare_pro(code, adj='qfq', start='2021-01-01', end='2022-04-01'):
# code:输入数字字符串,如‘300002’
# start和end输入'年-月-日'需转为'年月日'格式
ts.set_token('your are token')
pro = ts.pro_api()
pro = ts.pro_api('your are token')
if code.startswith('6'):
code = code + '.SH'
else:
code = code + '.SZ'
start = ''.join(start.split('-'))
end = ''.join(end.split('-'))
df = ts.pro_bar(ts_code=code, adj=adj, start_date=start, end_date=end)
# 原数据是倒序的,所以将时间设置为索引,根据索引重新排序
df.sort_values(by='trade_date',inplace=True)
return df
df = get_from_tushare_pro(scokt, adj='qfq', start = start, end = end)
#df.index=df['trade_date']
df3.index = range(df3.shape[0])
df.index = range(df.shape[0])
Tf=pd.concat([df,df3],ignore_index = True,axis=1)
Tf['liyi']=Tf.iloc[:,8:9].shift(-1)
print(Tf.columns)
# Tf.fillna(0)
Tf["liyi"] = Tf["liyi"].fillna(Tf["liyi"].mean())
Tf.drop(Tf.iloc[:,0:1],axis=1, inplace=True)
Tf.drop(Tf.iloc[:,0:1],axis=1, inplace=True)
Tf.drop(Tf.iloc[:,9:10],axis=1, inplace=True)
Tf.drop(Tf.iloc[:,14:15],axis=1, inplace=True)
Tf=Tf.iloc[380:,:]
print(Tf.to_csv('E:\东方财富今日涨停数据\东财涨停数据\云天化.csv'))