convert dataframe to dict, using a column name as key and another columns as k, v - python, pandas, dataframe

Viewed 162

Thanks in advance by all help.

I have a dataframe like this:

client_id product_id product_name timestamp_added product_constraint
abc fi2k loug 2021 null
abc fi2j feb 2021 null
abc fkn ros 2021 skn
cbd 01x get 2021 lok
cbd 018 los 2021 hnc
cbd 018 vng 2021 hnc

I need to do a dict by this df. Please note that client_id is not unique, event client_id as pk and product_id is not unique. Just the combination of client_id, product_id and product_constraint (that can be null in some cases) make a perfect unique identifier.

My dict needs to be like this:

{
    "abc":[
        {
            "product_id":"fi2k",
            "product_name": "loug",
            "timestamp_added": 2021,
            "product_constraint": null
        },
        {
            "product_id":"fi2j",
            "product_name": "feb",
            "timestamp_added": 2021,
            "product_constraint": null
        },
        {
            ...
        }
    ],
    "cbd":[
        {
            ...
        },
        {
            ...
        },
        {
            ...
        }
    ]
}

After this, i will use this dict to insert data into dynamodb.

1 Answers

try this:

    from pprint import pprint
    outdict = {} # create output dictionary

    df = df.set_index('client_id') # set index and group
    dfg = df.groupby(df.index)

    #iterate through groups populating the dictionary
    for grp in dfg.groups:
        g = dfg.get_group(grp)
        # print(g.to_dict('records'))
        outdict[grp] = g.to_dict('records')

    pprint(outdict)

{'abc': [{'product_constraint': nan,
          'product_id': 'fi2k',
          'product_name': 'loug',
          'timestamp_added': 2021},
         {'product_constraint': nan,
          'product_id': 'fi2j',
          'product_name': 'feb',
          'timestamp_added': 2021},
         {'product_constraint': 'skn',
          'product_id': 'fkn',
          'product_name': 'ros',
          'timestamp_added': 2021}],
 'cbd': [{'product_constraint': 'lok',
          'product_id': '01x',
          'product_name': 'get',
          'timestamp_added': 2021},
         {'product_constraint': 'hnc',
          'product_id': '018',
          'product_name': 'los',
          'timestamp_added': 2021},
         {'product_constraint': 'hnc',
          'product_id': '018',
          'product_name': 'vng',
          'timestamp_added': 2021}]}
Related