Home > Back-end >  split pandas column into many using delimiter from right to left
split pandas column into many using delimiter from right to left

Time:07-13

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)

CodePudding user response:

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
  • Related