不能将Json转换为panda

时间:2021-07-21 22:54:55

I have a json file like that:

我有一个json文件:

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

I tried to:

我想:

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

but got a df with a single column:

但是只有一栏的df:

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

What is the best way to read this json file to a pandas df?

将这个json文件读入熊猫df的最佳方式是什么?

1 个解决方案

#1


1  

You have to pass as argument only the inner object:

你必须只传递内部对象作为参数:

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

To load a json file:

要加载json文件:

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

Then you can do

然后你可以做

pd.DataFrame(data['info'])

#1


1  

You have to pass as argument only the inner object:

你必须只传递内部对象作为参数:

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

To load a json file:

要加载json文件:

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

Then you can do

然后你可以做

pd.DataFrame(data['info'])