繁体   English   中英

从嵌套字典创建多索引熊猫数据框

[英]Creating Multiindex Panda Dataframe from nested dict

我得到了这种形式的目录:

dict = {'Filter1':{'Method1':{'Fuction1':{'Value1': 1, 'Value2': 2},
                              'Fuction2':{'Value1': 1, 'Value2': 2}},
                   'Method2':{'Fuction1':{'Value1': 1, 'Value2': 2},
                              'Fuction2':{'Value1': 1, 'Value2': 2}}},
        'Filter2':{'Method1':{'Fuction1':{'Value1': 1, 'Value2': 2},
                              'Fuction2':{'Value1': 1, 'Value2': 2}},
                   'Method2':{'Fuction1':{'Value1': 1, 'Value2': 2},
                              'Fuction2':{'Value1': 1, 'Value2': 2}}}}

我想以这种形状创建一个数据框:

                      Filter1         Filter2
                Method1   Method2   Method1  Method2
Function1 Value1   1        1          1         1
          Value2   2        2          2         2
Function2 Value1   1        1          1         1
          Value2   2        2          2         2

我该怎么做呢? 我能找到的所有帖子都只是引用两个子行或子列,但从来没有同时引用这两个子行或子列。 先谢谢各位!!

到目前为止,我尝试过这种方法:

df = pd.DataFrame.from_dict({(i, j): dict[i][j]
                         for i in dict.keys()
                         for j in dict[i].keys()
                         },
                        orient='index')

女巫给了我这个结果:

                                       Fuction1                    Fuction2
Filter1 Method1  {'Value1': 1, 'Value2': 2}  {'Value1': 1, 'Value2': 2}
        Method2  {'Value1': 1, 'Value2': 2}  {'Value1': 1, 'Value2': 2}
Filter2 Method1  {'Value1': 1, 'Value2': 2}  {'Value1': 1, 'Value2': 2}
        Method2  {'Value1': 1, 'Value2': 2}  {'Value1': 1, 'Value2': 2}

但我希望行和列交换,Value1 和 Value2 作为行名

您想 1) 展平您的字典,2) 将其加载到数据帧中,3) 根据您的喜好重新格式化数据帧。

# 1. Flatten, eg using https://stackoverflow.com/questions/6027558/flatten-nested-dictionaries-compressing-keys
import collections
def flatten(dictionary, parent_key=False, separator='.'):
    """
    Turn a nested dictionary into a flattened dictionary
    :param dictionary: The dictionary to flatten
    :param parent_key: The string to prepend to dictionary's keys
    :param separator: The string used to separate flattened keys
    :return: A flattened dictionary
    """

    items = []
    for key, value in dictionary.items():
        new_key = str(parent_key) + separator + key if parent_key else key
        if isinstance(value, collections.MutableMapping):
            items.extend(flatten(value, new_key, separator).items())
        elif isinstance(value, list):
            for k, v in enumerate(value):
                items.extend(flatten({str(k): v}, new_key).items())
        else:
            items.append((new_key, value))
    return dict(items)

data_flat = flatten(data_dict)

# 2. Load it in pandas
df = pd.DataFrame.from_dict(data_flat, orient="index")

# 3. Reshape to your liking
df.index = pd.MultiIndex.from_tuples(df.index.str.split(".").map(tuple))
df = df.unstack(level=[0,1]).droplevel(axis=1, level=0) 

暂无
暂无

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

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