Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

Pandas Series.str.find() Method



TheSeries.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 Pythonstr.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 PandasSeries.str.find() method −

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

Parameters

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

TheSeries.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 theSeries.str.find() method.

import pandas as pd# Create a Series of stringss = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])# Find the index of the substring 'e' in each stringresult = 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             python1     Tutorialspoint2           articles3           Examplesdtype: objectIndexes of Substring 'e':0   -11   -12    63    6dtype: 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 stringss = pd.Series(['python', ' Tutorialspoint', 'articles', 'Examples'])# Find the index of the substring 't' within the range [2:10] in each stringresult = 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             python1     Tutorialspoint2           articles3           Examplesdtype: objectIndexes of Substring 't' within [2:10]:0    21    32    23   -1dtype: 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 stringss = pd.Series(['python', 'Tutorialspoint', 'articles', 'Examples'])# Find the index of the substring 'z' in each stringresult = 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           python1    Tutorialspoint2         articles3          Examplesdtype: objectIndexes of Substring 'z':0   -11   -12   -13   -1dtype: 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
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp