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

split pandas column into many using delimiter from right to left

The following code splits a column into two for example if I have the following string "hi.this.is.an.example" i will get separate columns for "hi" and "this".

This splits the string going from the left to right however, I want to go from right to left instead so would want "example" and "an"

df['variable'].str.split('.', 2, expand=True)

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 :

Like in pure python, use rsplit:

df = pd.DataFrame({'variable': ["hi.this.is.an.example"]})

df['variable'].str.rsplit('.', 2, expand=True)

output:

            0   1        2
0  hi.this.is  an  example
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