[英]Dataframe nested dictionary to multi-index dataframe
First to construct a simplified version of my question dataset, this is only helping to create a sample, I only have the final nest_dict on hand.首先构建我的问题数据集的简化版本,这只是帮助创建一个样本,我手头只有最终的nest_dict。 Meaning I dont have dfs separately:意思是我没有单独的dfs:
df1 = pd.DataFrame({'val':{'a': 1, 'b':2, 'c':3}})
df2 = pd.DataFrame({'val':{'a': 3, 'b':4, 'c':1}})
df3 = pd.DataFrame({'val':{'a': 2, 'b':1, 'c':4}})
dfs = [df1, df2, df3]
dates = ['2017-01-26','2017-02-03','2017-02-10']
nest_dict = {}
for date, d in zip(dates, dfs):
nest_dict[date] = d
I would like to convert it to a Multi-Index Dataframe.我想将其转换为多索引 Dataframe。
date1 a val1
b val2
c val3
date2 a val1
b val2
c val3
....
Its basically a dataframe with a date as key, it could easily become a panel, but since its been deprecated, how would I transform it to a multiindex dataframe?它基本上是一个以日期为键的 dataframe,它可以很容易地成为一个面板,但由于它已被弃用,我如何将它转换为多索引 dataframe? I did try following, but it does not work:我确实尝试了以下操作,但它不起作用:
d = {(key, item) for key, item in corr_SH50.items()}
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed
concat with keys:与键连接:
pd.concat(dfs,keys=dates)
val
2017-01-26 a 1
b 2
c 3
2017-02-03 a 3
b 4
c 1
2017-02-10 a 2
b 1
c 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.