簡體   English   中英

Pandas 如何用動態鍵壓平字典

[英]How can Pandas flatten a dict with dynamic keys

假設一個 dict 看起來像這樣:

example_1 = {
    'products': [
        {
            'p_code': 'AP001',
            'description': 'Product 1',
            'dimensions': {
                'height': 20,
                'width': 30
            }
        },
        {
            'p_code': 'AP002',
            'description': 'Product 2',
            'dimensions': {
                'height': 15,
                'width': 25
            }
        }
    ]
}

像這樣展平它是相當微不足道的:

df_ex_1 = pd.json_normalize(example_1, record_path=['products'])

結果:

p_code  description dimensions.height   dimensions.width
0   AP001   Product 1              20                 30
1   AP002   Product 2              15                 25

但是如果 dict 看起來像這樣怎么辦:

example_2 = {
    'AP001': {
        'description': 'Product 1',
        'dimensions': {
            'height': 20,
            'width': 30
        }        
    },
    'AP002': {
        'description': 'Product 2',
        'dimensions': {
            'height': 15,
            'width': 25
        }        
    }
}

有沒有辦法指定 record_path 需要下拉到它在運行時找到的每個根級鍵?

我不喜歡使用 json_normalize,所以任何其他能有效達到相同結果的方法都可以。

我試過了:

pd.DataFrame.from_dict(example_2, orient='index')

結果:

      description                    dimensions
AP001   Product 1   {'height': 20, 'width': 30}
AP002   Product 2   {'height': 15, 'width': 25}

但是,這不會使嵌套在維度中的數據變平。

如有必要,我可以編寫代碼來制作輸入字典的轉換版本,然后再將其拉入 Pandas,但我懷疑 Pandas 已經可以做我想做的事,我只是不知道怎么做。

請問有什么想法嗎?

使用pandas.concat和列表理解,您可以將每個DataFrame object 創建的pandas.json_normalize

df = pd.concat([pd.json_normalize(example_2[product]) for product in example_2], keys=example_2.keys())

這個方法的索引不是你所期望的,你可以做

df.index = example_2.keys()

得到你想要的。

Bijay Regmi提出的解決方案是最好的,但是,您也可以這樣做:

import pandas as pd

dict1 = {}
for key, value in example_2.items():
    if 'index' not in dict1.keys():
        dict1['index'] = []
    dict1['index'].append(key)
    for key2, value2 in value.items():
        if isinstance(value2, dict):
            for key3, value3 in value2.items():
                if key2 + '.' + key3 not in dict1.keys():
                    dict1[key2 + '.' + key3] = []
                dict1[key2 + '.' + key3].append(value3)
        else:
            if key2 not in dict1.keys():
                dict1[key2] = []
            dict1[key2].append(value2)

            
df = pd.DataFrame.from_dict(dict1)
df

   index description  dimensions.height  dimensions.width
0  AP001   Product 1                 20                30
1  AP002   Product 2                 15                25

暫無
暫無

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

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