Home > Blockchain >  pandas dataframe with just one column and hundreds of rows having a dictionary withh same keys but d
pandas dataframe with just one column and hundreds of rows having a dictionary withh same keys but d

Time:10-07

I have an excel file that has just one column with hundreds of rows. The column pattern is the same as that of a python dictionary, I want to convert it to multiple columns based on its keys. the column name is 0.

0 = [{'author': 'steve', 'age': '19', 'job': 'contractor'}, {'author': 'paul', 'age': '24', 'job': 'service'}]

What I want:

author   age   job
steve    19    contractor
paul     24    service

tell me the possible ways in python pandas data frame to convert it into multiple columns and export it into an excel file.

CodePudding user response:

Normalize semi-structured JSON data into a flat table.

import pandas as pd
import json
from pandas import json_normalize

data = {
  0 : [{'author': 'steve', 'age': '19', 'job': 'contractor'}, {'author': 'paul', 'age': '24', 'job': 'service'}]
}

df2 = json_normalize(data[0])
print(df2)

#Save file in excel
df2.to_excel("path")
#df2.to_excel("C:\file.xlsx")
  • Related