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

How to zip two columns into a key value pair dictionary in pandas

I have a dataframe with two related columns that needs to be merged into a single dictionary column.

Sample Data:

    skuId   coreAttributes.price    coreAttributes.amount
0   100     price                   8.84
1   102     price                   12.99
2   103     price                   9.99

Expected output:

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

skuId    coreAttributes
100      {'price': 8.84}
102      {'price': 12.99}
103      {'price': 9.99}

What I’ve tried:

planProducts_T = planProducts.filter(regex = 'coreAttributes').T
planProducts_T.columns = planProducts_T.iloc[0]
planProducts_T.iloc[1:].to_dict(orient = 'records')

I get UserWarning: DataFrame columns are not unique, some columns will be omitted. and this output:

[{'price': 9.99}]

Could you someone please help me on this.

>Solution :

You can use a list comprehension with python’s zip:

df['coreAttributes'] = [{k: v} for k,v in
                        zip(df['coreAttributes.price'],
                            df['coreAttributes.amount'])]

Output:

   skuId coreAttributes.price  coreAttributes.amount    coreAttributes
0    100                price                   8.84   {'price': 8.84}
1    102                price                  12.99  {'price': 12.99}
2    103                price                   9.99   {'price': 9.99}

If you need to remove the initial columns, use pop.

df['coreAttributes'] = [{k: v} for k,v in
                        zip(df.pop('coreAttributes.price'),
                            df.pop('coreAttributes.amount'))]

Output:

   skuId    coreAttributes
0    100   {'price': 8.84}
1    102  {'price': 12.99}
2    103   {'price': 9.99}
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