首页 > 解决方案 > 在python中将特定的字典格式抓取到数据框

问题描述

我有一个看起来像这样的字典结构:

data = [{'organization': {'id': 14, 'description': 'France'},
'distribution': True,
'forAttention': True},
{'organization': {'id': 38, 'description': 'Netherlands'},
'distribution': True,
'forAttention': True},
{'organization': {'id': 31, 'description': 'Poland'},
'notifying': True,
'origin': True},
{'organization': {'id': 17, 'description': 'United Kingdom'},
'distribution': True}]

(请注意,这只是 4 个组织的 1 个示例,但这个数字可能会有所不同)

我想将组织的这些信息放在数据框的一行中,如下所示:

>> df
Origin_ct      Notifying_ct     Distribution_ct       ForAttention_ct
Poland         Poland           France, Netherlands,  France, Netherlands
                                United Kingdom
                                

这个想法适用于每个组织 - 检查它是否具有 True 值并将其放在数据框的相应列中。我该怎么做呢?

标签: pythonpandasdataframedictionary

解决方案


尝试这个:

import pandas as pd
df = pd.DataFrame(columns=['Origin_ct','Notifying_ct','Distribution_ct','ForAttention_ct'])
origin_ct = []
notifying_ct = []
distribution_ct = []
forattention_ct = []
for organization in data:
    country = organization['organization']['description']
    if 'origin' in organization.keys() and organization['origin']:
        origin_ct.append(country)
    if 'notifying' in organization.keys() and organization['notifying']:
        notifying_ct.append(country)
    if 'distribution' in organization.keys() and organization['distribution']:
        distribution_ct.append(country)
    if 'forAttention' in organization.keys() and organization['forAttention']:
        forattention_ct.append(country)

推荐阅读