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

Converting pandas column with list of items to dictionary

I have DataFrame column like this:

col1
[DEBUG=true, DEBUG1=true]
[key1=true, key2=true]

This output DataFrame I want like this:

col1
{"DEBUG":"true", "DEBUG1": "true"}
{"key1":"true", "key2":"true"}

How can I achieve this?

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

>Solution :

Here’s one approach:

df['col1'] = df['col1'].explode().str.split('=').groupby(level=0).agg(list).apply(dict)

or you could use a list comprehension:

df['col1'] = [dict(x.split('=') for x in lst) for lst in df['col1'].tolist()]

Output:

                                  col1
0  {'DEBUG': 'true', 'DEBUG1': 'true'}
1     {'key1': 'true', 'key2': 'true'}
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