Reputation: 693
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
Upvotes: 0
Views: 281
Reputation: 5331
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.
Upvotes: 1
Reputation: 693
this did it. Thanks @ifly6
df['col1'] = df. col1.apply(lambda l: [s.split() for s in l])
Upvotes: 1