I put up a post on SO to convert a nested dataframe into Pandas. However, I don't understand the answer very well.
Here is my data -
nested_dict = {
'Marks': {
'Physics': {
'Theo': 99,
'Prac': 100
},
'Biology': {
'Theo': 89,
'Prac': 100
}
}
}
I want the output in this way -
Marks
Physics | Biology
Theo|Prac | Theo|Prac
99 | 100 | 89 | 100
Someone answered with the following code -
out = (pd.concat({k: pd.DataFrame(v)
for k, v in nested_dict.items()},
axis=1)
.unstack().to_frame().T
)
Here is what I understood -
for k, v in nested_dict.items() We are iterating through the dictionary with k as keys and v as values. But everything else seems confusing