0

I have this dataframe

d = {'parameters': [{'Year': '2018',
 'Median Age': 'nan',
 'Total Non-Family Household Income': 289.0,
 'Total Household Income': 719.0,
 'Gini Index of Income Inequality': 0.4121}]}
df_sample = pd.DataFrame(data=d)
df_sample.head()

I want to convert that json into pandas columns. How do I do this? Assume I only have the dataframe not the parameter d

I saw this example

#which columns have json
#device
json_cols = ['device', 'geoNetwork', 'totals',  'trafficSource']

for column in json_cols:

    c_load = test[column].apply(json.loads)
    c_list = list(c_load)
    c_dat = json.dumps(c_list)

    test = test.join(pd.read_json(c_dat))
    test = test.drop(column , axis=1)

But this does not seem too pythonic...

1 Answer 1

1

Use json_normalize:

df_sample = pd.json_normalize(data=d, record_path=['parameters'])

Resulting dataframe:

Year Median Age Total Non-Family Household Income Total Household Income Gini Index of Income Inequality
2018 nan 289.0 719.0 0.4121

UPD:

If you already have dataframe loaded, then applying pd.Series should work:

df_sample = df_sample['parameters'].apply(pd.Series) # or df_sample['parameters'].map(json.loads).apply(pd.Series) if data is not already dict
Sign up to request clarification or add additional context in comments.

1 Comment

Assume, I do not have the parameter d, what do I do then?

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.