0

split list elements into sub-elements in pandas dataframe this one is super close - but rather than split all elements and put into one list, I need to put them into a sublist.

df:

col1
['this is list 1', 'this is list 2', 'this is another list 3']
['this is list 1', 'this is list 2', 'this is another list 3']
['this is list 1', 'this is list 2', 'this is another list 3']

I am trying to make the dataframe column look like this:

col1
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]
[['this', 'is', 'list', '1'], ['this', 'is', 'list', '2'], ['this', 'is', 'another', lis't', '3']]

Basically split the elements in the list into sublists where each word is the elements/items in the sublist, all within a pandas df column

max
  • 671
  • 5
  • 13

2 Answers2

1

this did it. Thanks @ifly6

df['col1'] = df. col1.apply(lambda l: [s.split() for s in l])

max
  • 671
  • 5
  • 13
1

An apply solution would be

s.apply(lambda l: [s.split() for s in l])

I would leave it to someone else to think of a vectorised solution.

ifly6
  • 5,003
  • 2
  • 24
  • 47