如何将这个嵌套的JSON转换为DataFrame?

时间:2019-03-24 19:45:50

标签: python pandas dataframe

所以我有一段JSON代码,我想将其转换为DataFrame,但是我对DataFrames还是很陌生,所以有点卡住了。任何帮助将不胜感激:)

这是我的代码:

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)

这是输出:

{
    "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....

如您所见,它需要进行一些其他调整,我只想显示列“ email”,“ first_name”,“ id”和“ last_name”。有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您只需要选择行列表:

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