Reputation: 984
I have a dataframe where it has a column with categories and another column with words. Words can be repeated in the different categories, and therefore there can be duplicates. However, I would like to put these duplicates together in a dictionary, as follows:
category | word | score
happy | tolerance | 0.91
unhappy | tolerance | 0.12
angry | kill | 1.0
confident | tolerance | 0.56
friendly | tolerance | 0.70
happy | kill | 0.01
...
d = {
'tolerance' = {'happy': 0.91, 'angry': 0.01, 'confident': 0.56, 'friendly': 0.70},
'kill' = {'happy': 0.01, 'angry': 1.0, 'confident': 0.32, 'friendly': 0.016},
...
}
Upvotes: 0
Views: 44
Reputation: 5331
You could construct the resulting dict
directly, like so:
new_d = {}
for g, d in df.groupby('word'):
new_d[g] = d.set_index('category')['score'].to_dict()
The pd.DataFrame.to_dict
method, when passing orient='index'
does not support multiple index keys (if you set word
as index, for example).
Upvotes: 1