【发布时间】:2020-01-19 18:06:57
【问题描述】:
我有一个包含 500 多行的 csv,其中一列“_source”存储为 JSON。我想将其提取到熊猫数据框中。我需要每个键成为自己的列。
我有一个 1mb 的在线社交媒体数据 JSON 文件,我需要将字典和键值转换为它们自己的单独列。社交媒体数据来自 Facebook、Twitter/网络爬取...等。
大约有 528 行单独的帖子/推文/文本,每行在字典中都有许多字典。
我在下面附上了我的 Jupyter 笔记本中的几个步骤,以提供更完整的理解。我需要将字典中字典的所有键值对转换为数据框内的列。
我已经尝试通过这样做将其更改为数据框
source = pd.DataFrame.from_dict(source, orient='columns')
它返回类似这样的东西......我认为它可能会解压字典,但它没有。
source.head()
_source
0 {'sub_organization_id': 'default', 'uid': 'aba...
1 {'sub_organization_id': 'default', 'uid': 'ab0...
2 {'sub_organization_id': 'default', 'uid': 'ac0...
下面是形状
source.shape
(528, 1)
以下是“_source”的示例行。有许多字典和键:值对,其中每个键都需要是自己的列。
{
'sub_organization_id': 'default',
'uid': 'ac0fafe9ba98327f2d0c72ddc365ffb76336czsa13280b',
'project_veid': 'default',
'campaign_id': 'default',
'organization_id': 'default',
'meta': {
'rule_matcher': [{
'atribs': {
'website': 'github.com/res',
'source': 'Explicit',
'version': '1.1',
'type': 'crawl'
},
'results': [{
'rule_type': 'hashtag',
'rule_tag': 'Far',
'description': None,
'project_veid': 'A7180EA-7078-0C7F-ED5D-86AD7',
'campaign_id': '2A6DA0C-365BB-67DD-B05830920',
'value': '#Far',
'organization_id': None,
'sub_organization_id': None,
'appid': 'ray',
'project_id': 'CDE2F42-5B87-C594-C900E578C',
'rule_id': '1838',
'node_id': None,
'metadata': {
'campaign_title': 'AF',
'project_title': 'AF '
}
}
]
}
],
'render': [{
'attribs': {
'website': 'github.com/res',
'version': '1.0',
'type': 'Page Render'
},
'results': [{
'render_status': 'success',
'path': 'https://east.amanaws.com/rays-ime-store/renders/b/b/70f7dffb8b276f2977f8a13415f82c.jpeg',
'image_hash': 'bb7674b8ea3fc05bfd027a19815f82c',
'url': 'https://discooprdapp.com/',
'load_time': 32
}
]
}
]
},
'norm_attribs': {
'website': 'github.com/res',
'version': '1.1',
'type': 'crawl'
},
'project_id': 'default',
'system_timestamp': '2019-02-22T19:04:53.569623',
'doc': {
'appid': 'subtter',
'links': [],
'response_url': 'https://discooprdapp.com',
'url': 'https://discooprdapp.com/',
'status_code': 200,
'status_msg': 'OK',
'encoding': 'utf-8',
'attrs': {
'uid': '2ab8f2651cb32261b911c990a8b'
},
'timestamp': '2019-02-22T19:04:53.963',
'crawlid': '7fd95-785-4dd259-fcc-8752f'
},
'type': 'crawl',
'norm': {
'body': '\n',
'domain': 'discordapp.com',
'author': 'crawl',
'url': 'https://discooprdapp.com',
'timestamp': '2019-02-22T19:04:53.961283+00:00',
'id': '7fc5-685-4dd9-cc-8762f'
}
}
【问题讨论】:
标签: python json pandas dataframe dictionary