Python 读取 Json 文件作为 Pandas Dataframe 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36837663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:25:14  来源:igfitidea点击:

Reading Json file as Pandas Dataframe error

pythonjsonpandas

提问by Baktaawar

I have a Json file as follows. It's a list of dicts.

我有一个 Json 文件,如下所示。这是一个字典列表。

[{"city": "ab", "trips": 4, "date": "2014-01-25", "value": 4.7, "price": 1.1, "request_date": "2014-06-17", "medium": "iPhone", "%price": 15.4, "type": true, "Weekly_pct": 46.2, "avg_dist": 3.67, "avg_price": 5.0}, {"city": "bc", "trips": 0, "date": "2014-01-29", "value": 5.0, "price": 1.0, "request_date": "2014-05-05", "medium": "Android", "%price": 0.0, "type": false, "weekly_pct": 50.0, "avg_dist": 8.26, "avg_price": 5.0}.....]

When I read this using this:

当我使用这个阅读本文时:

data=pd.read_json('dataset.json')

I get the following error:

我收到以下错误:

ValueError: Expected object or value

ValueError:预期的对象或值

I tried this too:

我也试过这个:

from ast import literal_eval

with open('dataset.json') as f:
    data = literal_eval(f.read())

df = pd.DataFrame(data)

It gives the following error:

它给出了以下错误:

ValueError: malformed string

值错误:格式错误的字符串

Edit:

编辑:

Even Json.loads doesn't work. Tried this:

甚至 Json.loads 也不起作用。试过这个:

import json
data=json.loads('dataset.json')

ValueError: No JSON object could be decoded

ValueError:无法解码 JSON 对象

The Json file is 13.5MB but it seems to have huge amounts of data.

Json 文件是 13.5MB,但它似乎有大量的数据。

回答by jezrael

I think you can use modul jsonfor reading file.jsonand then DataFrame constructor:

我认为您可以使用 moduljson进行阅读file.json,然后DataFrame constructor

import pandas as pd
import json

with open('file.json') as f:
   data = json.load(f)
print data
[{u'city': u'ab', u'medium': u'iPhone', u'request_date': u'2014-06-17', u'price': 1.1, u'Weekly_pct': 46.2, u'value': 4.7, u'%price': 15.4, u'avg_price': 5.0, u'date': u'2014-01-25', u'avg_dist': 3.67, u'type': True, u'trips': 4}, {u'city': u'bc', u'medium': u'Android', u'request_date': u'2014-05-05', u'price': 1.0, u'weekly_pct': 50.0, u'value': 5.0, u'%price': 0.0, u'avg_price': 5.0, u'date': u'2014-01-29', u'avg_dist': 8.26, u'type': False, u'trips': 0}]

print pd.DataFrame(data)

   %price  Weekly_pct  avg_dist  avg_price city        date   medium  price  \
0    15.4        46.2      3.67        5.0   ab  2014-01-25   iPhone    1.1   
1     0.0         NaN      8.26        5.0   bc  2014-01-29  Android    1.0   

  request_date  trips   type  value  weekly_pct  
0   2014-06-17      4   True    4.7         NaN  
1   2014-05-05      0  False    5.0        50.0  

回答by Brad Solomon

You need to indicate to Pandas that "records" formatting (where the JSON appears like a list of dictionaries) is used in datasets.json.

您需要向 Pandas 指明在datasets.json.

res = pd.read_json('input/dataset.json', orient='records')

print(res.iloc[:, :5])
   %price  Weekly_pct  avg_dist  avg_price city
0    15.4        46.2      3.67          5   ab
1     0.0         NaN      8.26          5   bc

回答by MoKG

I had the same error. Turns out it couldn't find the file. I modified the path and pd.read_jsonworked fine. As for json.loads, thismight be helpful.

我有同样的错误。结果是找不到文件。我修改了路径并且pd.read_json工作正常。至于json.loads可能会有所帮助。

回答by embulldogs99

The following worked for me when pd.read_json failed: open file, load with normal json.load, then load into a pandas dataframe.

当 pd.read_json 失败时,以下对我有用:打开文件,使用正常的 json.load 加载,然后加载到 Pandas 数据帧中。

    import pandas as pd
    import json

    openfile=open('file.json')
    jsondata=json.load(openfile)
    df=pd.DataFrame(jsondata)

    openfile.close()
    print(df)