Home > Mobile >  Create nested dictionary from mulitple dataframe columns and Groupby
Create nested dictionary from mulitple dataframe columns and Groupby

Time:06-23

Original Dataframe example:

ColA     ColB     ColC
Dog      Red       8
Dog      Black     10
Dog      White     2
Dog      Blue      4
Cat      Red       7
Cat      Black     11
Cat      White     12
Cat      Blue      40

Desired dictionary

d =   {'Dog':{'Red': 8, 'Black':10, 'White':2,'Blue':4}, 
       'Cat':{'Red': 7, 'Black':11, 'White':12,'Blue':40}}

Any suggestions is appreciated!

CodePudding user response:

There might be more panda-esque ways, but this works:

result = {animal:dict(zip(subdf.ColB, subdf.ColC)) for animal, subdf in df.groupby('ColA')}

CodePudding user response:

Here's a way to do what you've asked:

dct = {}
df.apply(lambda x: dct[x.ColA].update({x.ColB : x.ColC}) if x.ColA in dct else dct.update({x.ColA : {x.ColB : x.ColC}}), axis=1)
print(dct)

Output:

{'Dog': {'Red': 8, 'Black': 10, 'White': 2, 'Blue': 4}, 'Cat': {'Red': 7, 'Black': 11, 'White': 12, 'Blue': 40}}

UPDATE:

You can also use defaultdict from the collections module:

from collections import defaultdict
dct = defaultdict(dict)
df.apply(lambda x: dct[x.ColA].update({x.ColB : x.ColC}), axis=1)
dct = dict(dct)
print(dct)
  • Related