python - 在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 值并将其放在数据框的相应列中。我该怎么做呢?
解决方案
尝试这个:
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)
推荐阅读
- php - PHP日期时间转换给了我错误的时间
- numpy - 用 Mathematica 和 Python 求解超越方程
- java - 堆栈如何在链接结构中工作?
- c++ - C++访问错误,指针故障
- mysql - pymysql 和 sqlalchemy: AttributeError: 'DateTime' 对象没有属性 'translate'
- mongodb - mongodb 通过 id 查找文档,然后根据 name 字段对结果进行分组
- javascript - 使用Javascript读取JSON对象内的数组内的对象
- r - 如何在变量列表上绘制图,同时将它们的名称保留在 r 中
- docker - hyperledger-fabric byfn.sh -m 失败,找不到脚本/scripts.sh
- c++ - long long int 类型的无效操作数和未解析的重载函数类型到二元运算符+