Pandas str.find()

The str.find() method in Pandas is used to search for a substring in each element of a Series, returning the lowest index at which the substring is found.

Example

import pandas as pd

# create a Series 
data = pd.Series(['hello world', 'pandas is fun', 'find the index'])

# use str.find() to find the index of 'is' in each string index_positions = data.str.find('is')
print(index_positions) ''' Output 0 -1 1 7 2 -1 dtype: int64 '''

str.find() Syntax

The syntax of the str.find() method in Pandas is:

Series.str.find(sub, start=0, end=None)

str.find() Arguments

The str.find() method takes following arguments:

  • sub - the substring to search for
  • start (optional) - the starting index within the string where the search should begin
  • end (optional) - the ending index within the string up to which the search is conducted

str.find() Return Value

The str.find() method returns a Series with integer values, representing the index of the first occurrence of the substring. If the substring is not found, -1 is returned.


Example1: Find Substring Positions Using str.find()

import pandas as pd

# create a Series
data = pd.Series(['sunny day', 'weekly meeting', 'holiday plans', 'birthday party'])

# use str.find() method to find the index of 'day' in each string index_positions = data.str.find('day')
print(index_positions)

Output

0    6
1   -1
2    4
3    5
dtype: int64

In the above example, we have used the str.find('day') method to find the index of day in each string.

It returns the lowest index where day is found, or -1 if not found.


Example 2: Case-Sensitive Search

import pandas as pd

# create a Series with mixed case strings
data = pd.Series(['Good Morning', 'EVENING star', 'afterNoon Walk'])

# search for a lowercase 'n' in each string index_positions = data.str.find('n')
print(index_positions)

Output

0    8
1   -1
2    8
dtype: int64

Here, str.find('n') searches for the lowercase letter n in the data Series where the strings have mixed cases.

The str.find() method is case-sensitive, so it will only find lowercase n.


Example 3: Search From a Specific Start Position

import pandas as pd

# create a Series
data = pd.Series(['repeat and repeat', 'Find and find again', 'cycle and recycle'])

# search for 'and' starting from index 6 index_positions = data.str.find('and', start=6)
print(index_positions)

Output

0    7
1   -1
2    6
dtype: int64

In this example, the search for the substring and starts from index 6 in each string. This means it will skip any occurrences of and before that index.


Example 4: Search With an End Position

import pandas as pd

# create a Series 
data = pd.Series(['beginning to end', 'start to finish', 'from start to end'])

# use str.find() to search for 'to' up to a specific end position index_positions = data.str.find('to', end=10)
print(index_positions)

Output

0   -1
1    6
2   -1
dtype: int64

Here, we used str.find() to search for the substring to in each string of the data Series.

The end=10 argument limits the search to the first b characters of each string. If to appears after the 10th character, it will not be found, and -1 will be returned.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges