lakeside
lakeside

Reputation: 145

Create a nested dictionary from a dataframe

I am trying to create a nested dict from a dataframe without including the column name (i.e., "description") as another level.

Here's what I am starting with:

d = {"field_name": ["foo", "foo", "foo", "bar", "bar"],
  "values": ["key1", "key2", "key3", "key1", "key5"],
 "description": ["value1", "value2", "value3", "value4", "value6"]}
df = pd.DataFrame(data=d)
df.head()
field_name values description
foo key1 value1
foo key2 value2
foo key3 value3
bar key1 value4
bar key5 value6

Here's what I currently have:

df.groupby("field_name")[["values","description"]].apply(lambda x: x.set_index("values").to_dict(orient="dict")).to_dict()

{'bar': {'description': {'key1': 'value4', 'key5': 'value6'}},
 'foo': {'description': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}}}

Here's what I want:

{'bar': {'key1': 'value4', 'key5': 'value6'},
     'foo': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}}

Upvotes: 0

Views: 253

Answers (1)

Danish Bansal
Danish Bansal

Reputation: 700

Following python code is the solution for your problem

import pandas as pd

d = {"field_name": ["foo", "foo", "foo", "bar", "bar"],
     "values": ["key1", "key2", "key3", "key1", "key5"],
     "description": ["value1", "value2", "value3", "value4", "value6"]}
df = pd.DataFrame(data=d)
print(df.values)

resultant_dict = {}
"""
df.values is like
[['foo' 'key1' 'value1']
 ['foo' 'key2' 'value2']
 ['foo' 'key3' 'value3']
 ['bar' 'key1' 'value4']
 ['bar' 'key5' 'value6']]
"""
for i in df.values:
    if i[0] in resultant_dict:
        resultant_dict[i[0]][i[1]] = i[2]
    else:
        resultant_dict[i[0]] = {i[1]: i[2]}

print(resultant_dict)

# Resultant Dict is {'foo': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}, 'bar': {'key1': 'value4', 
# 'key5': 'value6'}} 

Upvotes: 1

Related Questions