Sri Test
Sri Test

Reputation: 387

Converting string of strings to list in dataframe

I have a dataframe as following:

id|words
1|ant,bat,cat
2|mat,rat,pat

I want the words column to be as follows:

id|words
1|['ant','bat','cat']
2|['mat','rat','pat']

How could I do it? I tried with literaleval but it shows exception

Upvotes: 2

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 862611

Use Series.str.split:

df['words'] = df['words'].str.split(',')

Upvotes: 3

Related Questions