1

I didn't find anything in the Pandas documentations and cookbook (just references to CSV, and text files with separators) on JSON.

Is there an already defined function to load JSON directly into DataFrame? If there are different alternatives, which is the most efficient?

4
  • 2
    Can you post an example of what your JSON looks like please Commented May 8, 2013 at 15:45
  • Is not a particular instance question. I would like to know something that works for different structures. In a particular case, I could write an specific parser, iterating it, and pushing data in to the DF. Commented May 8, 2013 at 15:58
  • 1
    I was sure there was some discussion of a from_json method in the past, can't put my finger on it. Similar to this issue... Commented May 8, 2013 at 16:02
  • try this: stackoverflow.com/questions/15455388/… Commented May 8, 2013 at 16:57

3 Answers 3

11

with pandas 0.12:

import pandas as pd

d = pd.read_json('JSON File')
Sign up to request clarification or add additional context in comments.

Comments

9

The generic way to laod JSON to DataFrame is mentioned above:

import pandas as pd
d = pd.read_json('JSON File')

However, if your JSON file is nested and you need to create DataFrame of some nested attribute in it, one can use

 from pandas.io.json import json_normalize
 json_normalize(df[JSONKEYWORD])

In the JSONKEYWORD one can pass the nested JSON object and you get a sub Data Frame for that Nested JSON object.

Comments

0

Install pandasjson from github which provides DataFrame from_json and to_json classmethods.

https://github.com/pydata/pandasjson

import pandasjson
from pandas import DataFrame

"""
pinfo DataFrame.from_json
File:  ../lib/python2.7/site-packages/pandasjson.py
Definition: DataFrame.from_json(cls, json, orient='columns', dtype=None, numpy=True)

pinfo DataFrame.to_json
File:  ../lib/python2.7/site-packages/pandasjson.py
Definition: DataFrame.to_json(self, orient='columns', double_precision=10, force_ascii=True)
"""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.