Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a list column into multiple columns

I have a data frame where the last column is a column of lists. Below is how it looks:

Col1 | Col2 | ListCol
--------------------------
 na  |  na  | [obj1, obj2]
 na  |  na  | [obj1, obj2]
 na  |  na  | [obj1, obj2]

What I want is

Col1 | Col2 | Col3  | Col4
--------------------------
 na  |  na  | obj1  | obj2
 na  |  na  | obj1  | obj2
 na  |  na  | obj1  | obj2

I know that all the lists have the same amount of elements.

Edit:

Every element in ListCol is a list with two elements.

like image 996
Santi Avatar asked Jun 15 '18 19:06

Santi


People also ask

How do I split a column into multiple columns in list in pandas?

To split a pandas column of lists into multiple columns, create a new dataframe by applying the tolist() function to the column. The following is the syntax. You can also pass the names of new columns resulting from the split as a list.

How do I split a single column into multiple columns in Python?

We can use str. split() to split one column to multiple columns by specifying expand=True option. We can use str. extract() to exract multiple columns using regex expression in which multiple capturing groups are defined.

How do I split a column in a list in Python?

Use the str. split() Function to Split Strings Into Two List/Columns in Python Pandas. The string can be saved as a series list or constructed from a single, separated string, multiple column dataframes. Functions used are similar to Python's default split() method, but they can only be applied to a single string.


2 Answers

Currently, the tidyverse answer would be:

library(dplyr)
library(tidyr)
data %>% unnest_wider(ListCol)
like image 72
iago Avatar answered Sep 17 '22 20:09

iago


Here is one approach, using unnest and tidyr::spread...

library(dplyr)
library(tidyr)

#example df
df <- tibble(a=c(1, 2, 3), b=list(c(2, 3), c(4, 5), c(6, 7)))

df %>% unnest(b) %>% 
       group_by(a) %>% 
       mutate(col=seq_along(a)) %>% #add a column indicator
       spread(key=col, value=b)

      a   `1`   `2`
  <dbl> <dbl> <dbl>
1    1.    2.    3.
2    2.    4.    5.
3    3.    6.    7.
like image 28
Andrew Gustar Avatar answered Sep 17 '22 20:09

Andrew Gustar