str.rfind()
Pandas str.rfind()
is used to find a substring in each string, presented in the series on the right side. If a string is found, it returns the highest index of its occurrence. If no string is found, it will return -1.
Start and end points can also be passed to search for a specific portion of the string for a passed character or substring.
Syntax : Series.str.rfind (sub, start = 0, end = None)
Parameters:
sub: String or character to be searched in the text value in series
start: int value, start point of searching. Default is 0 which means from the beginning of string
end: int value, end point where the search needs to be stopped. Default is None.Return type: Series with Highest index position of substring occurrence
To load the CSV used in the code, press here.
In the following examples, the data frame used contains data for some NBA players. An image of the data frame before any operations is attached below.
Example # 1: Single character search
This example searches for one character & # 39; r & # 39; on the right side in each row of the Name column using the str.rfind () method. The start and end parameters are saved by default. The returned row is stored in a new column, so you can compare the indices by looking at them directly. Before using this method, null lines are removed with .dropna () to avoid errors.
|
Output:
As shown in the output image, the occurrence of the index in the Indexes column is equal to the position of the last occurrence of a character in the string. If the substring does not exist in the text, -1 is returned.
Example # 2: Search for a substring (more than one character)
In this example, the substring & # 39; ey & # 39; will be found in the Data Frame Name column. The start parameter is stored at 2 to start the search at the 3rd element (index position 2).
|
Output:
As shown in the output image, the highest or last index of occurrence of the substring is returned.