我有CSV格式的时间序列价格数据列表,内容如下:
asxList = ['ANZ', 'NAB', 'WBC']
for asxCode in asxList:
ohlcData = pd.DataFrame.from_csv(asxCode+'.CSV', header=0)
示例输出:
如何按特定顺序组装所有ohlcData,首先是DateTime索引,然后是asxList ['ANZ','NAB','WBC']索引,然后是数据列?
答案 0 :(得分:4)
创建数据框列表,为每个数据框添加code
列:
dfs = []
for asxCode in asxList:
df = pd.DataFrame.from_csv(asxCode+'.CSV', header=0)
df['code'] = asxCode
dfs.append(df)
连接数据框,将code
列添加到索引:
pd.concat(dfs).reset_index().set_index(['index', 'code'])
答案 1 :(得分:2)
与Dyz几乎相同,只使用keys
concat
asxList = ['ANZ', 'NAB', 'WBC']
l=[]
for asxCode in asxList:
l.append(pd.DataFrame.from_csv(asxCode+'.CSV', header=0))
pd.concat(l,keys=asxList)