无法将Json转换为pandas数据帧

时间:2018-05-08 22:43:13

标签: python json pandas

我有一个json这样的文件:

{
    "info": [
        {
            "product": "ABC",
            "email-adr": "abc08@gmail.com",
            "Gender": "M",
            "Name": "João",
            "id": "1069",
            "mobile": "iOS | 9.1",
            "client": " "
        },

我试图:

data2 = pd.read_json('file.json', encoding=str)

但是只有一列df

                      info
0  {u'id':u'1069',u'client'..    

将此json文件读取到pandas df的最佳方法是什么?

1 个答案:

答案 0 :(得分:1)

您必须仅作为参数传递内部对象:

import json

z="""{
"info":
[
{"product": "ABC", "email-adr": "abc08@gmail.com", "Gender": "M", "Name": "João", "id": "1069", "mobile": "iOS | 9.1", "client": " " }]}"""
>>> pd.DataFrame(json.loads(z)['info'])

  Gender  Name client        email-adr    id     mobile product
0      M  João         abc08@gmail.com  1069  iOS | 9.1     ABC

加载json文件:

with open("file.json", "r") as f:
  data = json.load(f)

然后你可以做

pd.DataFrame(data['info'])