user3058703
user3058703

Reputation: 591

Nesting a counter within another dictionary where keys are dataframe columns

I have a Pandas dataframe with a format like:

    ID      Code
    E1023   a
    E1023   b
    E1023   b
    E1023   b
    E1024   b
    E1024   c
    E1024   c

I would like to create a dictionary with the ID column as a key, with values from the Code column and its counts for a particular ID nested inside like:

{'E1023' : {'a' : 1, 'b' : 3 } , {'E1024' : {'b' : 1, 'c' : 2}}} 

I understand that I can use a Counter on the Code column, but how do I do this such that it is grouped by the ID and then nested within a dictionary where the ID is key?

Upvotes: 1

Views: 21

Answers (1)

jezrael
jezrael

Reputation: 862741

Use dictionary comprehension with DataFrame.groupby and Series.value_counts with Series.to_dict:

d = {k: v.value_counts().to_dict() for k, v in df.groupby('ID')['Code']}
print (d)
{'E1023': {'b': 3, 'a': 1}, 'E1024': {'c': 2, 'b': 1}}

Or use Counter and then convert to dict:

from collections import Counter
d = {k: dict(Counter(v)) for k, v in df.groupby('ID')['Code']}

Upvotes: 1

Related Questions