Change language

Python | Pandas Series.str.rfind ()

| |

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.

# pandas module import

import pandas as pd

 
# reading CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba.csv " )

  
# remove null columns to avoid errors

data.dropna (inplace = True )

  
# substring to search

sub = ’ r’

 
# create and transfer series in a new column

data [ "Indexes" ] = data [ "Name" ]. str . Rfind (sub)

 
# display
data

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).

# pandas module import

import pandas as pd

  
# read CSV file from URL

data = pd.read_csv ( " https://media.python.engineering/wp-content/upload/nba.csv " )

 
# remove null columns to avoid errors

data.dropna (inplace  = True )

  

 
# substring to search

sub = ’ey’

 
# start var

start = 2

 
# create and send a series to a new column

data [ "Indexes" ] = data [ " Name " ]. str . Rfind (sub, start )

 
# display
data

Output:
As shown in the output image, the highest or last index of occurrence of the substring is returned. 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method