简体   繁体   English

如何从复杂字典中创建 pandas dataframe?

[英]How do I create a pandas dataframe out of complex dictionary?

I have a dictionary with keys of type tuple and values of type list.我有一个字典,其中包含元组类型的键和列表类型的值。 for example:例如:

myDictionary= {('1', '1', '1', '1', '1'): [16.0, 75, 0.15, -99.0],
('1', '1', '1', '1', '2'): [16.0, 75, 0.15, -99.0],
('1', '1', '1', '2', '1'): [16.0, 75, 0.15, -99.0],
('1', '1', '1', '2', '2'): [16.0, 75, 0.15, -99.0],
('1', '1', '2', '1', '1'): [16.0, 75, 0.15, -99.0]
}

I would like to convert the above dictionary to pandas dataframe and add the header names for the indexes and values.我想将上面的字典转换为 pandas dataframe 并为索引和值添加 header 名称。 The final table should look like决赛桌应该是这样的

在此处输入图像描述

df=None
for i,(k,v) in enumerate(myDictionary.items()):
    if df is None:
        df=pd.DataFrame(columns=[f'id{k+1}' for k in range(len(k))]+[f'val{k+1}' for k in range(len(v))])
    df.loc[i]=list(k)+v

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM