指数整合
import akshare as ak import pandas as pd # 获取000300的历史行情数据 df_000300 = ak.index_zh_a_hist(symbol="000300") # 获取sh000852的历史行情数据 df_sh000852 = ak.stock_zh_index_daily(symbol="sh000852") # 获取HSI的历史行情数据 df_HSI= ak.stock_hk_index_daily_sina(symbol="HSI") # 获取IXIC的历史行情数据 df_IXIC = ak.index_us_stock_sina(symbol=".IXIC") df_Au = ak.spot_hist_sge(symbol='Au99.99') df_300etf = ak.stock_zh_index_daily_em(symbol="sh510300") df_1000etf = ak.stock_zh_index_daily_em(symbol="sh512100") df_hz = ak.stock_zh_index_daily_em(symbol="sh513600") # 获取黄金指数的历史行情数据 df_goldetf = ak.stock_zh_index_daily_em(symbol="sz159834") # 获取国债指数的历史行情数据 df_bondetf = ak.stock_zh_index_daily_em(symbol="sh511100") # 获取纳指指数的历史行情数据 df_nzetf = ak.stock_zh_index_daily_em(symbol="sz159509") # 将日期列转换为日期时间类型 df_000300['日期'] = pd.to_datetime(df_000300['日期']) # Ensure this column is datetime df_sh000852['date'] = pd.to_datetime(df_sh000852['date']) # Ensure this column is datetime df_HSI['date'] = pd.to_datetime(df_HSI['date']) # Ensure this column is datetime df_IXIC['date'] = pd.to_datetime(df_IXIC['date']) # Ensure this column is datetime df_Au['date'] = pd.to_datetime(df_Au['date']) # Ensure this column is datetime df_300etf['date'] = pd.to_datetime(df_300etf['date']) # Ensure this column is datetime df_1000etf['date'] = pd.to_datetime(df_1000etf['date']) # Ensure this column is datetime df_hz['date'] = pd.to_datetime(df_hz['date']) # Ensure this column is datetime df_goldetf['date'] = pd.to_datetime(df_goldetf['date']) # Ensure this column is datetime df_bondetf['date'] = pd.to_datetime(df_bondetf['date']) # Ensure this column is datetime df_nzetf['date'] = pd.to_datetime(df_nzetf['date']) # Ensure this column is datetime start_date='2024-12-01' end_date='2024-12-20' # 筛选出指定日期范围内的数据 df_sh000852 = df_sh000852[(df_sh000852['date'] >= start_date) & (df_sh000852['date'] <= end_date)] df_000300=df_000300[(df_000300['日期'] >= start_date) & (df_000300['日期'] <= end_date)] df_HSI=df_HSI[(df_HSI['date'] >= start_date) & (df_HSI['date'] <= end_date)] df_IXIC=df_IXIC[(df_IXIC['date'] >= start_date) & (df_IXIC['date'] <= end_date)] df_Au=df_Au[(df_Au['date'] >= start_date) & (df_Au['date'] <= end_date)] df_300etf=df_300etf[(df_300etf['date'] >= start_date) & (df_300etf['date'] <= end_date)] df_1000etf=df_1000etf[(df_1000etf['date'] >= start_date) & (df_1000etf['date'] <= end_date)] df_hz=df_hz[(df_hz['date'] >= start_date) & (df_hz['date'] <= end_date)] df_goldetf=df_goldetf[(df_goldetf['date'] >= start_date) & (df_goldetf['date'] <= end_date)] df_bondetf=df_bondetf[(df_bondetf['date'] >= start_date) & (df_bondetf['date'] <= end_date)] df_nzetf=df_nzetf[(df_nzetf['date'] >= start_date) & (df_nzetf['date'] <= end_date)] # 选取需要的列并进行重命名 df_000300 = df_000300[['日期', '收盘', '最高', '最低', '成交量']].rename(columns={ '日期': 'date', '收盘': '000300_close', '最高': '000300_high', '最低': '000300_low', '成交量': '000300_volume' }) df_sh000852 = df_sh000852[['date', 'close', 'high', 'low', 'volume']].rename(columns={ 'close': 'sh000852_close', 'high': 'sh000852_high', 'low': 'sh000852_low', 'volume': 'sh000852_volume' }) df_HSI = df_HSI[['date', 'close', 'high', 'low', 'volume']].rename(columns={ 'close': 'HSI_close', 'high': 'HSI_high', 'low': 'HSI_low', 'volume': 'HSI_volume' }) df_IXIC = df_IXIC[['date', 'close', 'high', 'low', 'volume']].rename(columns={ 'close': 'IXIC_close', 'high': 'IXIC_high', 'low': 'IXIC_low', 'volume': 'IXIC_volume' }) df_Au = df_Au[['date', 'close', 'high', 'low']].rename(columns={ 'close': 'Au_close', 'high': 'Au_high', 'low': 'Au_low' }) df_300etf = df_300etf[['date','close']].rename(columns={ 'close': '300etf_close'}) df_1000etf = df_1000etf[['date','close']].rename(columns={ 'close': '1000etf_close'}) df_hz = df_hz[['date','close']].rename(columns={ 'close': 'hz_close'}) df_goldetf = df_goldetf[['date','close']].rename(columns={ 'close': 'goldetf_close'}) df_bondetf = df_bondetf[['date','close']].rename(columns={ 'close': 'bondetf_close'}) df_nzetf = df_nzetf[['date','close']].rename(columns={ 'close': 'nzetf_close'}) # Merge the two DataFrames on the 'date' column for n,i in enumerate([df_000300, df_sh000852, df_HSI,df_IXIC,df_Au,df_300etf,df_1000etf,df_hz,df_goldetf,df_bondetf,df_nzetf]): if n == 0: result = i else: result = pd.merge(result, i, on='date', how='inner') # Display the merged DataFrame result
导出数据
df = pd.DataFrame(result) excel_file_name = "data_export.xlsx" df.to_excel(excel_file_name, index=False)