How can I turn this nested JSON into a DataFrame?

孤人 提交于 2021-01-29 08:00:37

问题


So I have a piece of JSON code and I want to turn it into a DataFrame, however I am quite new to DataFrames so I am a bit stuck. Any help would be appreciated :)

So this is my code:

data = response.json()

data_pretty = json.dumps(data, sort_keys=True, indent=4)
data_frame = pd.DataFrame(data)

# Pretty print
print(data_pretty)
print(data_frame)

This is the output:

{
    "status": "OK",
    "users": [
        {
            "email": "raf@webconexus.nl",
            "first_name": "Raf",
            "id": "24959",
            "last_name": "Rasenberg"
        },
        {
            "email": "raf.rasenberg@gmail.com",
            "first_name": "Raf",
            "id": "25795",
            "last_name": "Rasenberg"
        }
    ]
}
  status                                              users
0     OK  {'id': '24959', 'email': 'raf@webconexus.nl', ...
1     OK  {'id': '25795', 'email': 'raf.rasenberg@gmail....

As you can see it needs some additional tweaking, I only want to show the columns 'email', 'first_name', 'id' and 'last_name'. Can someone help me out?


回答1:


You only need to select list of rows:

pd.DataFrame(data.get('users'))


来源:https://stackoverflow.com/questions/55327861/how-can-i-turn-this-nested-json-into-a-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!