Home > Net >  How to unpack list values in DataFrame with the same amount of DataFrames
How to unpack list values in DataFrame with the same amount of DataFrames

Time:12-07

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.

CodePudding user response:

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])
  • Related