将 Dataframe 转换为嵌套字典

Turning a Dataframe into a nested dictionary

我有一个如下所示的数据框。我怎样才能把它放到像

这样的嵌套字典中
     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

我想将数据帧转换为以下字典格式:

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

例如:

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

我试过这个:

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

但如果客人有多个产品,它不会列出所有项目。

我也试过玩这个,但不太懂字典理解:

d = {k: v.groupby('GuestCode')['Invoice No','ProductName' , 'Quantity'].apply(list).to_dict() for k, v in df.groupby('Guest')}
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'])}

输出:

>>> 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
      }
    }
  }
}