I have a pandas dataframe with multiple headers. I am wondering how to convert this into a list of nested directories. Each row in the Pandas dataframe will be a nested dictionary in the list.
Here is an example
#Creaet an example multiheader dataframe
col =['id','x, single room','x, double room','y, single room','y, double room' ]
df = pd.DataFrame([[1,2,3,4,5], [3,4,7,5,3]], columns=col)
a = df.columns.str.split(', ', expand=True).values
#swap values in NaN and replace NAN to ''
df.columns = pd.MultiIndex.from_tuples([('', x[0]) if pd.isnull(x[1]) else x for x in a])
df
result
x y
id single room double room single room double room
0 1 2 3 4 5
1 3 4 7 5 3
This is the dataframe I want to convert to a list of nested dictionaries. So this is the desired result
[{'id': 1,
'x': {'double room': 3, 'single room': 2},
'y': {'double room': 5, 'single room': 4}},
{'id': 3,
'x': {'double room': 7, 'single room': 4},
'y': {'double room': 3, 'single room': 5}}]
In the code below, I directly create this list.
firstDict = { 'id':1, 'x':{'single room':2, 'double room':3}, 'y':{'single room':4, 'double room':5} }
secondDict = { 'id':3, 'x':{'single room':4, 'double room':7}, 'y':{'single room':5, 'double room':3} }
dictList = []
dictList.append( firstDict )
dictList.append( secondDict )
dictList
[{'id': 1,
'x': {'double room': 3, 'single room': 2},
'y': {'double room': 5, 'single room': 4}},
{'id': 3,
'x': {'double room': 7, 'single room': 4},
'y': {'double room': 3, 'single room': 5}}]
So in summary, how to I convert the dataframe df
to what dictList
is.
Edit:
This is a minimal example, the solution I am looking for should generalize to longer numbers of headers.