This is a list of dictionaries that I have which is to be converted to a dataframe. I tried using multi-index but couldn't convert the whole dataframe.
response = [{
"name": "xyz",
"empId": "007",
"details": [{
"address": [{
"street": "x street",
"city": "x city"
}, {
"street": "xx street",
"city": "xx city"
}],
"country": "xxz country"
},
{
"address": [{
"street": "y street",
"city": "y city"
}, {
"street": "yy street",
"city": "yy city"
}],
"country": "yyz country"
}
]
}]
I managed to get the inner list of dictionaries to a dataframe with the following code:
for i in details:
Country = i['country']
street =[]
city = []
index = pd.MultiIndex.from_arrays([[Country]*len(i['address']), list(range(1,len(i['address'])+1))], names=['Country', 'SL No'])
df=pd.DataFrame(columns=["Street","City"],index=index)
if i['address']:
for row in i['address']:
street.append(row['street'])
city.append(row['city'])
df["Street"]=street
df["City"]=city
frames.append(df)
df_final=pd.concat(frames)
Output obtained:
Country SL No Street City
xxz country 1 x street x city
2 xx street xx city
yyz country 1 y street y city
2 yy street yy city
How can I convert the list of dictionaries to a dataframe while keeping all the information?
The final output that I want:
Name EmpId Country Street City
xyz 007 xxz country x street x city
xx street xx city
yyz country y street y city
yy street yy cit