简体   繁体   English

如何将嵌套字典转换为pandas数据框?

[英]How to convert a nested dictionary to pandas dataframe?

I have a dictionary "my_dict" in this format: 我有以下格式的字典“ my_dict”:

{'l1':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}},
 'l2':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}}
}

Currently, I am using pd.DataFrame.from_dict(my_dict, orient='index') and get a df like this: 目前,我正在使用pd.DataFrame.from_dict(my_dict, orient='index')并获得如下所示的df:

                             c2                           c1
l1  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}
l2  {u'a': 3, u'c': 5, u'b': 4}  {u'a': 0, u'c': 2, u'b': 1}

However, what I want is both l1/l2 and c2/c3 as indexes and a/b/c as columns. 但是,我想要的是l1 / l2和c2 / c3作为索引,而a / b / c作为列。
Something like this: 像这样:

       a   b   c
l1 c1  0   1   2
   c2  3   4   5
l2 c1  0   1   2
   c2  3   4   5

What's the best way to do this? 最好的方法是什么?

Consider a dictionary comprehension to build a dictionary with tuple keys. 考虑使用字典理解来构建具有元组键的字典。 Then, use pandas' MultiIndex.from_tuples . 然后,使用pandas的MultiIndex.from_tuples Below ast is used to rebuild you original dictionary from string (ignore the step on your end). ast下面的内容用于从字符串重建原始字典(忽略最后的步骤)。

import pandas as pd
import ast

origDict = ast.literal_eval("""
{'l1':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}},
 'l2':{'c1': {'a': 0, 'b': 1, 'c': 2},
       'c2': {'a': 3, 'b': 4, 'c': 5}}
}""")

# DICTIONARY COMPREHENSION
newdict = {(k1, k2):v2 for k1,v1 in origDict.items() \
                       for k2,v2 in origDict[k1].items()}
print(newdict)
# {('l1', 'c2'): {'c': 5, 'a': 3, 'b': 4},
#  ('l2', 'c1'): {'c': 2, 'a': 0, 'b': 1},
#  ('l1', 'c1'): {'c': 2, 'a': 0, 'b': 1},
#  ('l2', 'c2'): {'c': 5, 'a': 3, 'b': 4}}

# DATA FRAME ASSIGNMENT
df = pd.DataFrame([newdict[i] for i in sorted(newdict)],
                  index=pd.MultiIndex.from_tuples([i for i in sorted(newdict.keys())]))    
print(df)
#        a  b  c
# l1 c1  0  1  2
#    c2  3  4  5
# l2 c1  0  1  2
#    c2  3  4  5

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

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