I’m looking for a way to convert a dataframe into a dictionary, very similar to what has been asked here:
Convert pandas DataFrame to a nested dict
Assuming a sample data frame
name v1 v2 v3
0 A A1 A11 1
1 A A2 A12 2
2 B B1 B12 3
3 C C1 C11 4
4 A A2 A21 6
5 A A2 A21 8
The number of columns may differ and so does the column names.
I’m looking to generate :
{
'A' : {
'A1' : { 'A11' : 1 },
'A2' : { 'A12' : 2 , 'A21' : 6 , 'A21' : 8 },
'B1' : {},
'C1' : {}
},
'B' : {
'A1' : {},
'A2' : {},
'B1' : { 'B12' : 3},
'C1' : {}
},
'C' : {
'A1' : {},
'A2' : {},
'B1' : {} ,
'C1' : { 'C11' : 4}
}
}
The method suggested elsewhere is via recursion:
def recur_dictify(frame):
if len(frame.columns) == 1:
if frame.values.size == 1: return frame.values[0][0]
return frame.values.squeeze()
grouped = frame.groupby(frame.columns[0])
d = {k: recur_dictify(g.ix[:,1:]) for k,g in grouped}
return d
Which gives:
>>> pprint.pprint(recur_dictify(df))
{'A': {'A1': {'A11': 1}, 'A2': {'A12': 2, 'A21': [6,8]}},
'B': {'B1': {'B12': 3}},
'C': {'C1': {'C11': 4}}}
But doesn’t replicate the empty dict nest at level v2 , and groups the repetition of A2 -A21 into array[6,8] . I’ve looked at Convert a Pandas DataFrame to a dictionary, no luck so far.
I assume that:
and df contains the output of your recur_dictify above:
You original dataframe is strange though. The B2 entry does not appear anywhere in your result.