Ohwh
Ohwh

Reputation: 23

Create a nested dictionary from a dataframe where the first column would be the key for the parent dictionary

I'm trying to created a nested dictionary from a pandas DataFrame. The table is structured as below and is a lookup table that I want to create a nested dictionary from for later use.

A B C
apple 1 happy
apple 2 sad
apple 3 not
pear 1 new
pear 2 old
liver 1 run
liver 2 fire
liver 3 old

The format of the dictionary needs to be as below as this will allow me use it to replace the key values in another data frame with their actual meaning. Where apple, pear, liver are the columns names and the numbers are the codes used instead of the actual values e.g. happy

d = {'apple':{1: 'happy', 2:'sad', 3:'not'}, 'pear':{1: 'new', 2: 'old'}, 'liver':{1: 'run', 2:'fire', 3:'old'}}

Upvotes: 1

Views: 462

Answers (1)

n1colas.m
n1colas.m

Reputation: 3989

Use Pandas groupby over the column A, then, iterate over the groups to create the dictionary. Using the dictionary comprehension combined with the built-in function zip, use columns B and C as keys and values, respectively.

...
g = df.groupby('A')

d = {}
for n,dfg in g:
    d[n] = {k:v for k,v in zip(dfg['B'],dfg['C'])}

print(d)

Output from d

{'apple': {1: 'happy', 2: 'sad', 3: 'not'}, 'liver': {1: 'run', 2: 'fire', 3: 'old'}, 'pear': {1: 'new', 2: 'old'}}

Upvotes: 1

Related Questions