Open In App

Python | Pandas Series.str.rpartition()

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas str.rpartition() works in a similar way like str.partition() and str.split(). Instead of splitting string on every occurrence from left side, .rpartition() splits string only once and that too reversely (From right side). Unlike .split() method, the rpartition() method stores the separator/delimiter too.

.str has to be prefixed every time before calling this method to differentiate it from the Python’s default function otherwise, it will throw an error.

Note: This method is different from the str.partition() method, Instead of splitting at the first occurrence, the string is splitted at the last occurrence of separator/delimiter.

Syntax: Series.str.rpartition(pat=’ ‘, expand=True)

Parameters:
pat: String value, separator or delimiter to separate string at. Default is ‘ ‘ (whitespace)
expand: Boolean value, returns a data frame with different value in different columns if True. Else it returns a series with list of strings. Default is True.

Return type: Series of list or Data frame depending on expand Parameter

To download the CSV used in code, click here.

In the following examples, the data frame used contains data on some NBA players. The image of data frame before any operations is attached below.

 
Example #1: Splitting string into list

In this example, the Team column is splitted into list on last occurrence of ‘o’. Before doing any operations, null rows are removed using .dropna() method to avoid errors.

Python3




# importing pandas module 
import pandas as pd 
    
# making data frame 
    
# removing null values to avoid errors 
data.dropna(inplace = True
  
# splitting and overwriting column
data["Team"]= data["Team"].str.rpartition("o", False)
  
# display
data


Output:
As shown in the output image, the string ‘Boston Celtics’ was separated on last occurrence of ‘o’. Also the returned list is having separator too.

 

Example #2: Splitting string into data frame

In this example, the Name column is splitted into data frame on last occurrence (First from right side) of ‘a’ by keeping expand Parameter True. Before doing any operations, null rows are removed using .dropna() method to avoid errors.

Python3




# importing pandas module 
import pandas as pd 
    
# making data frame 
    
# removing null values to avoid errors 
data.dropna(inplace = True
  
# splitting and overwriting column
df = data["Name"].str.rpartition("a", True)
  
# display
df


Output:
As shown in the output image, the string was splitted into data frame on the last occurrence of ‘a’ in string.

Note: If there is no occurrence of separator in string, the whole string is stored in the last column of data frame/last index of list.



Last Updated : 13 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads