Noobie Noobie - 6 months ago 136
Python Question

how to load a json into a pandas dataframe?

I am using a

REST API
to get a
json
file as follows:

import urllib2
import pandas as pd
import numpy as np
import requests

request='myrequest'
data= requests.get(request)
json=data.json()
df=pd.DataFrame(json)


and the dataframe looks like

items
0 {u'access': u'all', u'count': 501, u'time': 2014}
1 {u'access': u'all', u'count': 381, u'time': 2015}


How can I transform this single column (that looks like a dictionary) into proper columns in Pandas?

EDIT

the raw json data looks like this

{
"items": [
{
"access": "all",
"count": 200,
"time": 2015
},
{
"access": "all",
"count": 14,
"time": 2015
},
]
}


Thanks!

Answer

pd.read_json(json_str)

Here is the docs.

EDIT:

For a list of json str you can also just:

import json
import Pandas as pd

df = pd.DataFrame.from_records(map(json.loads, json_lst))