close

How to flatten a pandas dataframe with some columns as json?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to flatten a pandas dataframe with some columns as json in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to flatten a pandas dataframe with some columns as json?

  1. How to flatten a pandas dataframe with some columns as json?

    create a custom function to flatten columnB then use pd.concat
    def flatten(js): return pd.DataFrame(js).set_index('pos').squeeze()

  2. flatten a pandas dataframe with some columns as json

    create a custom function to flatten columnB then use pd.concat
    def flatten(js): return pd.DataFrame(js).set_index('pos').squeeze()

Method 1

The quickest seems to be:

import pandas as pd
import json

json_struct = json.loads(df.to_json(orient="records"))    
df_flat = pd.io.json.json_normalize(json_struct) #use pd.io.json

Method 2

create a custom function to flatten columnB then use pd.concat

def flatten(js):
    return pd.DataFrame(js).set_index('pos').squeeze()

pd.concat([df.drop(['columnA', 'columnB'], axis=1),
           df.columnA.apply(pd.Series),
           df.columnB.apply(flatten)], axis=1)
enter image description here

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read