Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Json file as Pandas Dataframe error

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

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

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:

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

ValueError: No JSON object could be decoded

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

like image 596
Baktaawar Avatar asked Apr 25 '16 10:04

Baktaawar


People also ask

How do I load a JSON file into a Pandas DataFrame?

Reading JSON Files using Pandas To read the files, we use read_json() function and through it, we pass the path to the JSON file we want to read. Once we do that, it returns a “DataFrame”( A table of rows and columns) that stores data.

Can Pandas handle JSON?

This API from Pandas helps to read JSON data and works great for already flattened data like we have in our Example 1. You can download the JSON from here.

How do I read a JSON file in Python?

Reading From JSON It's pretty easy to load a JSON object in Python. Python has a built-in package called json, which can be used to work with JSON data. It's done by using the JSON module, which provides us with a lot of methods which among loads() and load() methods are gonna help us to read the JSON file.


2 Answers

I think you can use modul json for reading file.json and then 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  
like image 152
jezrael Avatar answered Oct 14 '22 04:10

jezrael


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

like image 20
MoKG Avatar answered Oct 14 '22 06:10

MoKG