Json to DataFrame with Pandas

This week I need to grab a JSON file (from my Mongo database) and put this info in a Dataframe. Pandas makes this supprisingly easy, of course. Here is the code.

import pandas as pd
import json

with open('./datajson') as data_file:
    data = json.load(data_file)
    df = pd.io.json.json_normalize(data)

We simply load the json file, parse the json with the build in Python json library and then use the json_normalize to convert the documents. Super easy.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s