在新的多索引级别下串联 pandas 列 [英] Concatenate Pandas columns under new multi-index level
问题描述
给出一个数据帧字典,例如:
Given a dictionary of data frames like:
dict = {'ABC': df1, 'XYZ' : df2} # of any length...
其中每个数据框具有相同的列和相似的索引,例如:
where each data frame has the same columns and similar index, for example:
data Open High Low Close Volume
Date
2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833
2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866
2002-01-21 0.19523 0.20970 0.19162 0.20608 771149
最简单的方法是将所有数据帧组合为一个,并具有如下所示的多索引:
What is the simplest way to combine all the data frames into one, with a multi-index like:
symbol ABC XYZ
data Open High Low Close Volume Open ...
Date
2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 ...
2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866 ...
2002-01-21 0.19523 0.20970 0.19162 0.20608 771149 ...
我尝试了几种方法-例如,对于每个数据帧,用.from_product(['ABC', columns])
之类的多索引替换列,然后沿axis=1
进行连接,但没有成功.
I've tried a few methods - eg for each data frame replace the columns with a multi-index like .from_product(['ABC', columns])
and then concatenate along axis=1
, without success.
推荐答案
您可以使用concat
进行操作(keys
参数将创建层次结构列索引):
You can do it with concat
(the keys
argument will create the hierarchical columns index):
d = {'ABC' : df1, 'XYZ' : df2}
print pd.concat(d.values(), axis=1, keys=d.keys())
XYZ ABC \
Open High Low Close Volume Open High
Date
2002-01-17 0.18077 0.18800 0.16993 0.18439 1720833 0.18077 0.18800
2002-01-18 0.18439 0.21331 0.18077 0.19523 2027866 0.18439 0.21331
2002-01-21 0.19523 0.20970 0.19162 0.20608 771149 0.19523 0.20970
Low Close Volume
Date
2002-01-17 0.16993 0.18439 1720833
2002-01-18 0.18077 0.19523 2027866
2002-01-21 0.19162 0.20608 771149
真的concat
想要列表,所以以下等效:
Really concat
wants lists so the following is equivalent:
print(pd.concat([df1, df2], axis=1, keys=['ABC', 'XYZ']))
这篇关于在新的多索引级别下串联 pandas 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!