Pandas Series.str.index() Method



The Series.str.index() method in Python Pandas is used to find the position of the first occurrence of a specified substring in each string of a Series or a Column of a DataFrame. If the substring is not found, it raises a ValueError.

This method is similar to the standard Python str.index() method, and it is useful for string searching operations, ensuring that missing substrings are explicitly handled.

Syntax

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

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

Parameters

The Series.str.index() 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.index() method returns a Series or Index of objects representing the lowest indexes where the substring is found. If the substring is not found, it raises a ValueError for those elements.

Example 1

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

import pandas as pd

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

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

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
dtype: object

Indexes of Substring 'e':
0    2
1    2
2    2
dtype: int64

Example 2

This example demonstrates finding the lowest index of a substring 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'])

# Find the index of the substring 't' within the range [2:10] 
result = s.str.index('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
dtype: object

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

Example 3

This example demonstrates the behavior of the Series.str.index() method when the substring is not present in some elements, raising a ValueError.

import pandas as pd

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

try:
    # Find the index of the substring 'z' in each string
    result = s.str.index('z')
    print("Indexes of Substring 'z':")
    print(result)
except ValueError as e:
    print(f"ValueError: {e}")

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

ValueError: substring not found

The ValueError is raised because the substring 'z' is not present in any of the string elements.

python_pandas_working_with_text_data.htm
Advertisements