Skip to content Skip to sidebar Skip to footer

Convert Json Nested List Of Dict To Dataframe

How can I convert the following list of dicts (json output) to a pandas DataFrame. I tried res = {} for d in list_of_dict: res.update(d) It gives me the error: ValueError: d

Solution 1:

Something like this will work:

I have assumed your json object is one large string named 'data'.

import pandas as pd    
import json

# json object:
json_string = """ { "PlanCoverages": [ { "PlanId": 65860, ... """# 1) load json object as python variable:
data = json.loads(json_string)

# 2) convert to dataframe:
plan_coverages = pd.DataFrame(data['PlanCoverages'])

Solution 2:

I think you need something like this

import pandas as pd    
import json


convert json to python dictionary:
my_dict = json.loads(json_string)

convert a dictionary my_dict to dataframe df:
df = pd.concat({k: pd.DataFrame(v).T for k, v in my_dict.items()}, axis=0)

#reset the indexdf = df.reset_index()

Post a Comment for "Convert Json Nested List Of Dict To Dataframe"