Y4RD13
Y4RD13

Reputation: 984

how to convert all pandas duplicates to a dictionary?

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:

Dataframe

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

...

Expected dictionary:

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

Answers (1)

ifly6
ifly6

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

Related Questions