简体   繁体   English

如何将嵌套字典转换为水平字典 Pandas Dataframe

[英]How to convert nested dictionary to levelled Pandas Dataframe

How to convert more than 3 level N nested dictionary to levelled dataframe?如何将超过 3 个 N 级嵌套字典转换为水平 dataframe?

input_dict = {
                '.Stock': {
                            '.No[0]': '3241512)',
                            '.No[1]': '1111111111',
                            '.No[2]': '444444444444',
                            '.Version': '46',
                            '.Revision': '78'
                          },
                '.Time': '12.11.2022'
             }

what I expect:我期望的是:

import pandas as pd
expected_df = pd.DataFrame([{'level_0': '.Stock', 'level_1': '.No_0', "value": '3241512'},
 {'level_0': '.Stock', 'level_1': '.No_1', "value": '1111111111',},
 {'level_0': '.Stock', 'level_1': '.No_2', "value": '444444444444'},
 {'level_0': '.Stock', 'level_1': '.Version', "value": '46'},
 {'level_0': '.Stock', 'level_1': '.Revision', "value": '78'},
 {'level_0': '.Time',  "value": '12.11.2022'}])
index指数 level_0 level_0 level_1 1级 value价值
0 0 .Stock 。库存 .No_0 .No_0 3241512 3241512
1 1个 .Stock 。库存 .No_1 .No_1 1111111111 1111111111
2 2个 .Stock 。库存 .No_2 .No_2 444444444444 444444444444
3 3个 .Stock 。库存 .Version 。版本 46 46
4 4个 .Stock 。库存 .Revision 。修订 78 78
5 5个 .Time 。时间 NaN 12.11.2022 12.11.2022

Firsly I need to convert nested dictionary to list of levelled dictionaries, than lastly convert list of dictionaries to dataframe. How can I convert, pls help me!首先我需要将嵌套字典转换为水平字典列表,最后将字典列表转换为 dataframe。我该如何转换,请帮助我!

I've already tried the code below but it doesn't show exactly the right result.我已经尝试了下面的代码,但它没有显示完全正确的结果。

pd.DataFrame(input_dict).unstack().to_frame().reset_index()

You can first flatten your nested dictionary with a recursive function (see "Best way to get nested dictionary items" ).您可以先使用递归 function 展平嵌套字典(请参阅“获取嵌套字典项的最佳方式” )。

def flatten(ndict):
    def key_value_pairs(d, key=[]):
        if not isinstance(d, dict):
            yield tuple(key), d
        else:
            for level, d_sub in d.items():
                key.append(level)
                yield from key_value_pairs(d_sub, key)
                key.pop()
    return dict(key_value_pairs(ndict))
>>> input_dict = {
        '.Stock': {
            '.No[0]': '3241512)',
            '.No[1]': '1111111111',
            '.No[2]': '444444444444',
            '.Version': '46',
            '.Revision': '78'
            },
        '.Time': '12.11.2022'
    }
>>> d = flatten(input_dict)
>>> d
{('.Stock', '.No[0]'): '3241512)',
 ('.Stock', '.No[1]'): '1111111111',
 ('.Stock', '.No[2]'): '444444444444',
 ('.Stock', '.Version'): '46',
 ('.Stock', '.Revision'): '78',
 ('.Time',): '12.11.2022'}

You then need to fill missing levels, as for the last row in your example.然后,您需要填写缺失的级别,就像示例中的最后一行一样。 You can use zip_longest for the purpose and also stick the values to the last position.您可以为此目的使用zip_longest ,并将值粘贴到最后一个 position。

>>> from itertools import zip_longest
>>> d = list(zip(*zip_longest(*d.keys()), d.values()))
>>> d
[('.Stock', '.No[0]', '3241512)'),
 ('.Stock', '.No[1]', '1111111111'),
 ('.Stock', '.No[2]', '444444444444'),
 ('.Stock', '.Version', '46'),
 ('.Stock', '.Revision', '78'),
 ('.Time', None, '12.11.2022')]

Now you can create your dataframe:现在您可以创建您的 dataframe:

>>> pd.DataFrame(d)
    0   1   2
0   .Stock  .No[0]  3241512)
1   .Stock  .No[1]  1111111111
2   .Stock  .No[2]  444444444444
3   .Stock  .Version    46
4   .Stock  .Revision   78
5   .Time   None    12.11.2022

I found solution, thanks for your comments:(我找到了解决方案,感谢您的意见:(

def nesting_list_convert(in_dict,level=0):
    out_list = []
    for k1, v1 in in_dict.items():
        if isinstance(v1, dict):
            temp_list = nesting_list_convert(v1,level+1)
            for element in temp_list:
                temp_dict = {("level_"+str(level)) : k1}
                temp_dict.update(element)
                out_list.append(temp_dict)
        else:
             out_list.append({("level_"+str(level)) : k1,"value":v1})
return out_list

out_df = pd.DataFrame(nesting_list_convert(input_dict))
out_df = out_df.reindex(sorted(out_df.columns), axis=1)
index指数 level_0 level_0 level_1 1级 value价值
0 0 .Stock 。库存 .No_0 .No_0 3241512 3241512
1 1个 .Stock 。库存 .No_1 .No_1 1111111111 1111111111
2 2个 .Stock 。库存 .No_2 .No_2 444444444444 444444444444
3 3个 .Stock 。库存 .Version 。版本 46 46
4 4个 .Stock 。库存 .Revision 。修订 78 78
5 5个 .Time 。时间 NaN 12.11.2022 12.11.2022

This solves 6' nested level of dictionary.这解决了 6' 嵌套级别的字典。

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

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