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 unpack list values in DataFrame with the same amount of DataFrames

I would like to unpack DataFrame, which looks like that (sample data):
enter image description here

As you can see there are for example 2 users who are packed in this data frame. I want to have output with as many Data Frames as I have users in the lists. So in this example I would like to have 2 Data Frames with values like that (example for the first one in the list):
enter image description here

And another one of course with values from the second elements of the lists. I was trying to use explode method, but it is not working as I expected.

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 :

Use DataFrame.apply for processing each column and select first value of tuples in indexing:

#if there are lists columns
df = df.apply(lambda x: x.str[0])

Or first remove [ and split by comma before indexing:

#if there are strings columns
df = df.apply(lambda x: x.str.strip('[').str.split(',').str[0])
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