I have this 4 level dict of ice cream sales in different countries:
Import pandas as pd
from operator import add
d1={
'Sweden':{'jan':{
'0-5': 5,
'6-8': 8,
'9-10':19,
'11-15': 14,
'16-18': 24},
'march':{
'0-5': 5,
'6-8': 18,
'9-10': 9,
'11-15': 14,
'16-18': 24},
'feb':{
'0-5': 5,
'6-8': 7,
'9-10': 3,
'11-15': 14,
'16-18': 24}},
'Norway':{'jan':{
'0-5': 25,
'6-8': 8,
'9-10': 45,
'11-15': 14,
'16-18': 24},
'march':{
'0-5': 2,
'6-8': 8,
'9-10': 88,
'11-15': 14,
'16-18': 24},
'feb':{
'0-5': 5,
'6-8': 48,
'9-10': 9,
'11-15': 39,
'16-18': 24}}
}
I can unpack it to my desired DataFrame using a nested for loop:
colnames=['country','month','age','revenue']
lst=[]
for i in d1.keys():
for j in d1[i].keys():
revenue=list(d1[i][j].items())
l1=list(map(add,[(i,j)]*5,revenue))
lst=lst+l1
df=pd.DataFrame.from_records(lst,columns=colnames)
to a shape (30,4) DataFrame.
Does pandas have a built in function for doing this in a nicer/faster way without for loops? What is the fastest way to do this?
You can use pandas functions to reshape, but it's likely less efficient:
Or:
A variant of your code using a dictionary comprehension, which is faster than pandas:
Output:
Timings: