Pandas Series.str.find() Method



The Series.str.find() method in Pandas is used to return the lowest index in each string in the Series where a specified substring is found. If the substring is not found, it returns -1.

This method is equivalent to the standard Python str.find() method. It is useful for locating substrings within strings in a Pandas Series or columns of a DataFrame, providing flexibility with the optional start and end parameters.

Syntax

Following is the syntax of the Pandas Series.str.find() method −

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

Parameters

The Series.str.find() method accepts the following parameters −

  • sub − A string representing the substring being searched for.

  • start − An optional integer, default is 0. It represents the left edge index from where the search starts.

  • end − An optional integer, default is None. It represents the right edge index up to which the search is performed.

Return Value

The Series.str.find() method returns a Series or Index of integers representing the lowest indexes where the substring is found. If the substring is not found, it returns -1 for those elements.

Example 1

This example demonstrates finding the lowest index of a sub-string in each string element in a Series using the Series.str.find() method.

import pandas as pd

# Create a Series of strings
s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])

# Find the index of the substring 'e' in each string
result = s.str.find('e')

print("Input Series:")
print(s)
print("\nIndexes of Substring 'e':")
print(result)

When we run the above code, it produces the following output −

Input Series:
0             python
1     Tutorialspoint
2           articles
3           Examples
dtype: object

Indexes of Substring 'e':
0   -1
1   -1
2    6
3    6
dtype: int64

Example 2

This example demonstrates finding the lowest index of a sub-string within a specified range in each string element in a Series.

import pandas as pd

# Create a Series of strings
s = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])

# Find the index of the substring 't' within the range [2:10] in each string
result = s.str.find('t', start=2, end=10)

print("Input Series:")
print(s)
print("\nIndexes of Substring 't' within [2:10]:")
print(result)

When we run the above code, it produces the following output −

Input Series:
0             python
1     Tutorialspoint
2           articles
3           Examples
dtype: object

Indexes of Substring 't' within [2:10]:
0    2
1    3
2    2
3   -1
dtype: int64

The specified range [2:10] is used to limit the search for the substring 't' within each string element.

Example 3

This example demonstrates finding the lowest index of a substring in each string element in a Series when the substring is not present in some elements.

import pandas as pd

# Create a Series of strings
s = pd.Series(['python', 'Tutorialspoint', 'articles', 'Examples'])

# Find the index of the substring 'z' in each string
result = s.str.find('z')

print("Input Series:")
print(s)
print("\nIndexes of Substring 'z':")
print(result)

When we run the above code, it produces the following output −

Input Series:
0           python
1    Tutorialspoint
2         articles
3          Examples
dtype: object

Indexes of Substring 'z':
0   -1
1   -1
2   -1
3   -1
dtype: int64

A value of -1 for all elements indicates that the substring 'z' is not present in any of the string elements.

python_pandas_working_with_text_data.htm
Advertisements