Home > Net >  How to split text in column if they are enumerated?
How to split text in column if they are enumerated?

Time:10-15

I have a dataframe with one column "col":

col
1) wake up 2) brush your teeth 3) go to school
1. save 2. download
this is a great day
...

How could i split those texts putting each enumerated text in new row? so desired result is:

col
wake up 
brush your teeth 
go to school
save
download
this is a great day
...

How could I do that? What should i write in str.split("") ?

CodePudding user response:

here is one way to do it

split the values using regex on digit followed by ) or .
df['col']=df['col'].str.split(r'\d[\)|\.]',regex=True)

#explode the resulting list into separate rows
df=df.explode('col')

#get rid of empty lines
df=df[df['col'].str.strip().ne('')]
df
    col
1   wake up
1   brush your teeth
1   go to school
2   save
2   download
3   this is a great day
  • Related