kumarb
kumarb

Reputation: 49

text spliting of column based on comma in python

I want to split the text column (hashtag). I have used the split function but creating another column doesnt help.

ID     date        hashtag
123  1/1/1990     #cause,#contain
345  1/3/1990     #abc, #ghy,#hhh,#

output:

ID     date        hashtag
123  1/1/1990     #cause
123  1/1/1990     #contain
345  1/3/1990     #abc
345  1/3/1990     #ghy
345  1/3/1990    #hhh
345  1/3/1990     #

Upvotes: 0

Views: 45

Answers (1)

mozway
mozway

Reputation: 260420

Assuming this is pandas (based on your previous questions), you can split the hashtag with a regex (,\s* = comma with optional spaces) and then explode the column:

(df.assign(hashtag=df['hashtag'].str.split(',\s*'))
   .explode('hashtag')
)

output:

    ID      date   hashtag
0  123  1/1/1990    #cause
0  123  1/1/1990  #contain
1  345  1/3/1990      #abc
1  345  1/3/1990      #ghy
1  345  1/3/1990      #hhh
1  345  1/3/1990         #

Upvotes: 3

Related Questions