簡體   English   中英

Python:包含多個字典的解包列表

[英]Python: Unpacking list containing multiple dictionaries

我得到了這個列表,其中包含多個光盤和列表。

[{'classification': 
 {'description': 'A registered charge', 
 'type': 'charge-description'}, 
 'charge_code': 'SC3802280001', 
 'etag': '157167f8f780f440048f4056da17784dfafe64e5', 
 'delivered_on': '2015-09-04', 
 'persons_entitled': [{'name': 'The Royal Bank of Scotland PLC'}], 
 'created_on': '2015-09-03', 
 'links': {'self': '/company/SC380228/charges/IKH-4F5A4YmihSPe9D8Mq-WAJDw'}, 
 'particulars': {'floating_charge_covers_all': True, 
 'contains_negative_pledge': True, 
 'contains_floating_charge': True}, 
 'status': 'outstanding', 
 'transactions': [{'links': {'filing': '/company/SC380228/filing-history/MzEzMDM4OTgxOGFkaXF6a2N4'}, 
 'filing_type': 'create-charge-with-deed', 
 'delivered_on': '2015-09-04'}], 
 'charge_number': 1
}]

我需要把它拆開,讓它看起來像一個基本的 DF。 示例如下:

在此處輸入圖像描述

有什么建議我該怎么做?

所以你在這里面臨的問題是你的數據很混亂——具體來說,它有很多層次,所以它不太適合二維 dataframe。

例如,考慮一下,如果你把它分解成所有的組成層次,那本字典會是什么樣子:

data = [
    {
        'classification': {
            'description': 'A registered charge', 
            'type': 'charge-description',
        }, 
        'charge_code': 'SC3802280001', 
        'etag': '157167f8f780f440048f4056da17784dfafe64e5', 
        'delivered_on': '2015-09-04',
        'persons_entitled': [
            {
                'name': 'The Royal Bank of Scotland PLC',
            }
        ], 
        'created_on': '2015-09-03',
        'links': {
            'self': '/company/SC380228/charges/IKH-4F5A4YmihSPe9D8Mq-WAJDw',
        }, 
        'particulars': {
            'floating_charge_covers_all': True, 
            'contains_negative_pledge': True, 
            'contains_floating_charge': True,
        }, 
        'status': 'outstanding', 
        'transactions': [
            {
                'links': {
                    'filing': '/company/SC380228/filing-history/MzEzMDM4OTgxOGFkaXF6a2N4',
                },
                'filing_type': 'create-charge-with-deed', 
                'delivered_on': '2015-09-04',
            }
        ], 
        'charge_number': 1,
    }
]

沒有明顯的列級別,也沒有明顯的值級別,因此您最終會得到一些 dataframe 單元格,它們本身就是鍵/值對的字典(其值也可以是鍵值對。)。

我們可以在這里做的一件事是遞歸地遍歷字典以獲取字典每個分支的最低級別的鍵/值對:

def unpack(item):

    # For list items, loop over the elements and unpack them
    if isinstance(item, list):

        root_dict = {}

        for elem in item:
            root_dict.update(unpack(elem))

    # For dict items, loop over the keys and values and either 
    # unpack them or store them
    if isinstance(item, dict):
        sub_dict = {}

        for key, value in item.items():

            # If the value needs further unpacking, unpack it and add it
            # to our sub_dict
            if isinstance(value, dict) or isinstance(value, list):
                sub_dict.update(unpack(value))

            # If we've reached the bottom level, add key/value to sub_dict
            else:
                sub_dict[key] = [value]
        
        # Send the sub_dict back up through the stack (or return it and 
        # end execution if what we passed in didn't contain any lists)
        return sub_dict
    
    # Return the root_dict that we've been updating with our sub_dict items
    # (assuming what we passed in contained lists)
    return root_dict

遞歸 function 將返回一個字典,該字典遵循簡單的一鍵單值結構,即:

unpacked_dict = {
    'description': ['A registered charge'], 
    'type': ['charge-description'],
    'charge_code': ['SC3802280001'], 
    'etag': ['157167f8f780f440048f4056da17784dfafe64e5'], 
    'delivered_on': ['2015-09-04'],
    'name': ['The Royal Bank of Scotland PLC'],
    'created_on': ['2015-09-03'],
    'self': ['/company/SC380228/charges/IKH-4F5A4YmihSPe9D8Mq-WAJDw'],
    'floating_charge_covers_all': [True], 
    'contains_negative_pledge': [True], 
    'contains_floating_charge': [True],
    'status': ['outstanding'], 
    'filing': ['/company/SC380228/filing-history/MzEzMDM4OTgxOGFkaXF6a2N4'],
    'filing_type': ['create-charge-with-deed'], 
    'delivered_on': ['2015-09-04'],
    'charge_number': [1],
}

然后,您可以使用pd.DataFrame(unpacked_dict)將該解壓縮的字典傳遞給 Pandas 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM