Skip to content
Advertisement

Convert nested dictionary to pandas dataframe

I have a nested dictionary as below:

JavaScript

I need to convert it into a dataframe like below

JavaScript

I have tired the following code from this answer

JavaScript

I am getting the dataframe like below

JavaScript

This is the closest I got to the desired output. What changes do I need to make to the code to get the desired dataframe?

Advertisement

Answer

Change dictionary first and then pass to Series with reshape by Series.unstack:

JavaScript

Another idea is created dictionary of tuples in keys with defaultdict:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement