Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split text in column if they are enumerated?

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("") ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading