Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Turning a Dataframe into a nested dictionary

I have a dataframe like below. How can I get it into a nested dictionary like

     Guest  GuestCode          ProductName  Quantity Invoice No
0    Maria        NaN       Pro Plus Cream         2  OBFL22511
1    Maria        NaN  Soothe Stress Cream         1  OBFL22511
2  Sanchez  OBFLG3108       Pro Plus Cream         1  OBFL22524
3    Karen  OBFLG1600  Soothe Stress Cream         1  OBFL22525
4    Karen  OBFLG1600       Pro Plus Cream         1  OBFL22525

I want the dataframe converted into the following dictionary format:

{"Guest": {"GuestCode": {"Invoice No": {"ProductName": Quantity}}}

For example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{"Karen": {"OBFLG160": {"OBFL22525": {"Soothe Stress Cream": 1, "Pro Plus Cream": 1}}}

I tried this:

for index, row in df.iterrows():
    my_dict[row['Guest']] = {row['GuestCode']: {row['Invoice No']: {row['ProductName']}}}

But it does not list all the items if a guest has multiple products.

I also tried and played around with this, but don’t really understand dictionary comprehension:

d = {k: v.groupby('GuestCode')['Invoice No','ProductName' , 'Quantity'].apply(list).to_dict() for k, v in df.groupby('Guest')}

>Solution :

my_dict = {k[0]: {k[1]: {k[2]: {p: q for p, q in row[['ProductName', 'Quantity']].values}}} for k, row in df.fillna('<NA>').groupby(['Guest', 'GuestCode', 'Invoice No'])}

Output:

>>> my_dict
{'Karen': {'OBFLG1600': {'OBFL22525': {'Soothe Stress Cream': 1, 'Pro Plus Cream': 1}}},
 'Maria': {'<NA>': {'OBFL22511': {'Pro Plus Cream': 2, 'Soothe Stress Cream': 1}}},
 'Sanchez': {'OBFLG3108': {'OBFL22524': {'Pro Plus Cream': 1}}}}

>>> import json
>>> print(json.dumps(my_dict, indent=2))
{
  "Karen": {
    "OBFLG1600": {
      "OBFL22525": {
        "Soothe Stress Cream": 1,
        "Pro Plus Cream": 1
      }
    }
  },
  "Maria": {
    "<NA>": {
      "OBFL22511": {
        "Pro Plus Cream": 2,
        "Soothe Stress Cream": 1
      }
    }
  },
  "Sanchez": {
    "OBFLG3108": {
      "OBFL22524": {
        "Pro Plus Cream": 1
      }
    }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading