Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a list in a Pandas cell into multiple columns [duplicate]

Tags:

I have a really simple Pandas dataframe where each cell contains a list. I'd like to split each element of the list into it's own column. I can do that by exporting the values and then creating a new dataframe. This doesn't seem like a good way to do this especially, if my dataframe had a column aside from the list column.

import pandas as pd  df = pd.DataFrame(data=[[[8,10,12]],                         [[7,9,11]]])  df = pd.DataFrame(data=[x[0] for x in df.values]) 

Desired output:

   0   1   2 0  8  10  12 1  7   9  11 

Follow-up based on @Psidom answer:

If I did have a second column:

df = pd.DataFrame(data=[[[8,10,12], 'A'],                         [[7,9,11], 'B']]) 

How do I not loose the other column?

Desired output:

   0   1   2  3  0  8  10  12  A 1  7   9  11  B 
like image 498
user2242044 Avatar asked Dec 02 '16 03:12

user2242044


1 Answers

You can loop through the Series with apply() function and convert each list to a Series, this automatically expand the list as a series in the column direction:

df[0].apply(pd.Series)  #   0    1   2 #0  8   10  12 #1  7    9  11 

Update: To keep other columns of the data frame, you can concatenate the result with the columns you want to keep:

pd.concat([df[0].apply(pd.Series), df[1]], axis = 1)  #   0    1   2  1 #0  8   10  12  A #1  7    9  11  B 
like image 104
Psidom Avatar answered Sep 28 '22 09:09

Psidom