'I have a dataframe
df
Key Value
0 key1 value1
1 key2 value2
2 key3 value3
3 key1 value4
4 key5 value5
5 key2 value1
6 key3 value2
...
n keyn valuen
I want to group all the values for the same key and hence tried to convert to dictionary as
pd.Series(df.Value.values,index=df.Key).to_dict()
But this would overwrite the key's value with the latest record
for eg
{'key1':'value5','key2':'value1','key3':'value2','key5':'value5'}
Instead of
{'key1':['value1','value4','value5'],'key2':['value2','value1'],'key3':['value3','value2'],'key5':'value5'}
I tried changing the orientation of to_dict as
pd.Series(df.Value.values,index=df.Key).to_dict('list')
But it threw the error
TypeError: unsupported type: <class 'str'>
CodePudding user response:
You can aggregate the values as a list on groupby, then export as dict:
df.groupby('Key')['Value'].agg(list).to_dict()
Result:
{'key1': ['value1', 'value4'],
'key2': ['value2', 'value1'],
'key3': ['value3', 'value2'],
'key5': ['value5']}
CodePudding user response: