Pandas Series: str.rpartition() function
Series-str.rpartition() function
The str.rpartition() function is used to split the string at the last occurrence of sep.
This method splits the string at the last occurrence of sep, and returns 3 elements containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return 3 elements containing two empty strings, followed by the string itself.
Syntax:
Series.str.rpartition(self, sep=' ', expand=True)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
sep | String to split on. | str or compiled regex | Required |
pat | str Default Value: whitespace |
Required | |
expand | If True, return DataFrame/MultiIndex expanding dimensionality. If False, return Series/Index. | bool Default Value: True |
Required |
Returns: DataFrame/MultiIndex or Series/Index of objects
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s
Output:
0 Albert Junior Labuschagne 1 Jason Pitt-Rivers dtype: object
Example - To partition by the last space instead of the first one:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(['Albert Junior Labuschagne', 'Jason Pitt-Rivers'])
s.str.rpartition()
Output:
0 1 2 0 Albert Junior Labuschagne 1 Jason Pitt-Rivers
Previous: Series-str.replace() function
Next: Series-str.rstrip() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-str-rpartition.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics