如何将来自api的嵌套json解压到数据帧中?

2024-04-26 10:30:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在发出一个API GET请求并返回一个unicode json响应。我可以将响应读入数据帧,但有一列正在嵌套。当我将此列加载到数据帧中时,是否仍要取消对此列的注释?你知道吗

迄今为止的代码:

import requests
import pandas as pd

headers = {
    'Authorization': 'Bearer 123',
}

r = requests.get('url', headers=headers)

df = pd.read_json(r.text)

嵌套列的数据如下所示:

df.nested_data
{u'landing_id': u'xxx', u'landed_at': u'2018-10-03T17:44:33Z', u'answers': [{u'field': {u'type': u'picture_choice', u'ref': xxx, u'id': u'xxx'}, u'type': u'choice', u'choice': {u'label': u'gum'}}, {u'field': {u'type': u'picture_choice', u'ref': u'xxx, u'id': u'xxxx'}, u'type': u'choice', u'choice': {u'label': u'Great'}}, {u'field': {u'type': u'picture_choice', u'ref': u'xxxx', u'id': u'xxx'}, u'type': u'choice', u'choice': {u'label': u'Cool as ice'}}], u'token': u'xxx7d', u'submitted_at': u'2018-10-03T17:45:59Z', u'metadata': {u'network_id': u'axxx', u'platform': u'mobile', u'referer': u'xxx', u'user_agent': u'Mozilla/5.0 (Linux; Android 8.0.0; MHA-L29 Build/xxx-L29) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Mobile Safari/537.36', u'browser': u'touch'}}

如何取消此列的注释,使其按行解压?你知道吗

非常感谢。你知道吗


Tags: 数据importrefidjsonfieldastype