I have a pandas dataframe df
that looks like this
name value1 value2
A 123 1
B 345 5
C 712 4
B 768 2
A 318 9
C 178 6
A 321 3
I want to convert this into a dictionary with name
as a key and list of dictionaries (value1 key and value2 value) for all values that are in name
So, the output would look like this
{
'A': [{'123':1}, {'318':9}, {'321':3}],
'B': [{'345':5}, {'768':2}],
'C': [{'712':4}, {'178':6}]
}
So, far I have managed to get a dictionary with name
as key and list of only one of the values as a list by doing
df.set_index('name').transpose().to_dict(orient='list')
How do I get my desired output? Is there a way to aggregate all the values for the same name
column and get them in the form I want?
df
? $\endgroup$