create dictionary without removing duplicates from dataframe
{k: g.to_dict(orient='records') for k, g in df.groupby(level=0)}
# {'bob': [{'age': 20, 'name': 'bob'}, {'age': 30, 'name': 'bob'}],
# 'jim': [{'age': 25, 'name': 'jim'}]}
4
8
df.groupby(level=0).apply(lambda x: x.to_dict('r')).to_dict()
# {'bob': [{'age': 20, 'name': 'bob'}, {'age': 30, 'name': 'bob'}],
# 'jim': [{'age': 25, 'name': 'jim'}]}
Thank you!
8
0
Are there any code examples left?
New code examples in category Python