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?

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
Luchux
  • 803
  • 1
  • 7
  • 17
  • 2
    Can you post an example of what your JSON looks like please – Ryan Saxe May 08 '13 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. – Luchux May 08 '13 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](https://github.com/pydata/pandas/issues/1067)... – Andy Hayden May 08 '13 at 16:02
  • try this: http://stackoverflow.com/questions/15455388/dict-of-dicts-of-dicts-to-dataframe/15455455#15455455 – Jeff May 08 '13 at 16:57

3 Answers3

11

with pandas 0.12:

import pandas as pd

d = pd.read_json('JSON File')
erantdo
  • 685
  • 2
  • 9
  • 19
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.

arpiagar
  • 4,314
  • 1
  • 19
  • 12
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)
"""
James
  • 9
  • 1