Contents

Split String on Last Occurrence of Character in pandas

You can use rpartition() and rsplit() functions from pandas to split a string on the last occurrence of a character in Python.

The following examples explain how you can split the string based on the last occurrence of a character in pandas DataFrame.

Using rpartition() function

This example explains how to split a string on the last occurrence of a character using the rpartition() function.

Create a sample pandas DataFrame,

# import package
import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3, 4, 6], 	
	'col2': ['A-B-C', 'B-D-E', 'X-Y-Z', 'C-D-Z', 'D-E-F']})

# view DataFrame
df

   col1   col2
0     1  A-B-C
1     2  B-D-E
2     3  X-Y-Z
3     4  C-D-Z
4     6  D-E-F

Split the col2 string with a last occurrence of - character.

df["col2"].str.rpartition(sep="-")

# output
     0  1  2
0  A-B  -  C
1  B-D  -  E
2  X-Y  -  Z
3  C-D  -  Z
4  D-E  -  F

By default, the rpartition() function splits the string into three columns (part before and after the separator, and the separator itself).

You can see that the last columns contain the split value based on the last occurrence of - character.

Using rsplit() function

This example explains how to split a string on the last occurrence of a character using the rsplit() function.

Create a sample pandas DataFrame,

# import package
import pandas as pd

df = pd.DataFrame({'col1': [1, 2, 3, 4, 6], 
	'col2': ['A-B-C', 'B-D-E', 'X-Y-Z', 'C-D-Z', 'D-E-F']})

# view DataFrame
df

   col1   col2
0     1  A-B-C
1     2  B-D-E
2     3  X-Y-Z
3     4  C-D-Z
4     6  D-E-F

Split the col2 string with the last occurrence of - character.

df["col2"].str.rsplit("-").str[-1]

# output
0    C
1    E
2    Z
3    Z
4    F

Here, rsplit() function splits the string based on - character and then we return the element of the string after the last occurrence of - character using str[-1].

You can see that the output contain the split value based on the last occurrence of - character.