close

How to convert JSON data inside a pandas column into new columns

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to convert JSON data inside a pandas column into new columns 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 convert JSON data inside a pandas column into new columns?

  1. How to convert JSON data inside a pandas column into new columns?

    I was running into issues because the json in my data frame column was kept as a string and not as an object. I had to add the additional step of mapping the column:

  2. convert JSON data inside a pandas column into new columns

    I was running into issues because the json in my data frame column was kept as a string and not as an object. I had to add the additional step of mapping the column:

Method 1

If you already have your data in acList column in a pandas DataFrame, simply do:

import pandas as pd
pd.io.json.json_normalize(df.acList[0])

Alt AltT    Bad CMsgs   CNum    Call    CallSus Cou EngMount    EngType ... Sqk TSecs   TT  Tisb    TrkH    Trt Type    VsiT    WTC Year
0   NaN 0   False   1   7503    NaN False   United States   1   3   ...     3   a   False   False   1   CRJ2    0   2   2001
1   8025.0  0   False   35445   NaN TEST1234    False   United States   0   0   ... 0262    75229   a   False   False   1   NaN 0   0   NaN

Since pandas 1.0 the imports should be:

import pandas as pd
pd.json_normalize(df.acList[0])

Method 2

 I was running into issues because the json in my data frame column was kept as a string and not as an object. I had to add the additional step of mapping the column:

import json
import pandas as pd
pd.io.json.json_normalize(df.acList.apply(json.loads))

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