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.
str.rpartition() works in a similar way like
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 spliiting at the first occurrence, the string is splitted at the last occurrence of separator/delimiter.
Syntax: Series.str.rpartition(pat=’ ‘, expand=True)
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.
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.
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.
- Python | pandas.map()
- Python | Pandas Series.all()
- Python | Pandas DatetimeIndex.day
- Python | Pandas DatetimeIndex.second
- Python | Pandas Series.sum()
- Python | Pandas DataFrame.loc
- Python | Pandas Index.where
- Python | Pandas Series.abs()
- Python | Pandas Series.get()
- Python | Pandas Series.ptp()
- Python | Pandas Series.pop()
- Python | Pandas Series.max()
- Python | Pandas Series.min()
- Python | Pandas Series.pow()
- Python | Pandas Timestamp.second
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.