I have a dataframe as below:
Id | C1 | C2
---|----|----
1 | 1 | 12
1 | 2 | 15
2 | 4 | 20
2 | 5 | 25
I am trying to groupby
'Id' and construct a dictionary for each 'Id'
df = df.groupby('Id')
dt = df.to_dict()
I get an error:
AttributeError: Cannot access callable attribute 'to_dict' of 'DataFrameGroupBy' objects, try using the 'apply' method
I am trying to get an output(list of dictionaries) as:
{Id: 1: {C1: [1, 2],
C2: [12, 15]},
Id: 2: {C1: [4, 5],
C2: [20, 25]}
}