
- Python Pandas - Home
- Python Pandas - Introduction
- Python Pandas - Environment Setup
- Python Pandas - Basics
- Python Pandas - Introduction to Data Structures
- Python Pandas - Index Objects
- Python Pandas - Panel
- Python Pandas - Basic Functionality
- Python Pandas - Indexing & Selecting Data
- Python Pandas - Series
- Python Pandas - Series
- Python Pandas - Slicing a Series Object
- Python Pandas - Attributes of a Series Object
- Python Pandas - Arithmetic Operations on Series Object
- Python Pandas - Converting Series to Other Objects
- Python Pandas - DataFrame
- Python Pandas - DataFrame
- Python Pandas - Accessing DataFrame
- Python Pandas - Slicing a DataFrame Object
- Python Pandas - Modifying DataFrame
- Python Pandas - Removing Rows from a DataFrame
- Python Pandas - Arithmetic Operations on DataFrame
- Python Pandas - IO Tools
- Python Pandas - IO Tools
- Python Pandas - Working with CSV Format
- Python Pandas - Reading & Writing JSON Files
- Python Pandas - Reading Data from an Excel File
- Python Pandas - Writing Data to Excel Files
- Python Pandas - Working with HTML Data
- Python Pandas - Clipboard
- Python Pandas - Working with HDF5 Format
- Python Pandas - Comparison with SQL
- Python Pandas - Data Handling
- Python Pandas - Sorting
- Python Pandas - Reindexing
- Python Pandas - Iteration
- Python Pandas - Concatenation
- Python Pandas - Statistical Functions
- Python Pandas - Descriptive Statistics
- Python Pandas - Working with Text Data
- Python Pandas - Function Application
- Python Pandas - Options & Customization
- Python Pandas - Window Functions
- Python Pandas - Aggregations
- Python Pandas - Merging/Joining
- Python Pandas - MultiIndex
- Python Pandas - Basics of MultiIndex
- Python Pandas - Indexing with MultiIndex
- Python Pandas - Advanced Reindexing with MultiIndex
- Python Pandas - Renaming MultiIndex Labels
- Python Pandas - Sorting a MultiIndex
- Python Pandas - Binary Operations
- Python Pandas - Binary Comparison Operations
- Python Pandas - Boolean Indexing
- Python Pandas - Boolean Masking
- Python Pandas - Data Reshaping & Pivoting
- Python Pandas - Pivoting
- Python Pandas - Stacking & Unstacking
- Python Pandas - Melting
- Python Pandas - Computing Dummy Variables
- Python Pandas - Categorical Data
- Python Pandas - Categorical Data
- Python Pandas - Ordering & Sorting Categorical Data
- Python Pandas - Comparing Categorical Data
- Python Pandas - Handling Missing Data
- Python Pandas - Missing Data
- Python Pandas - Filling Missing Data
- Python Pandas - Interpolation of Missing Values
- Python Pandas - Dropping Missing Data
- Python Pandas - Calculations with Missing Data
- Python Pandas - Handling Duplicates
- Python Pandas - Duplicated Data
- Python Pandas - Counting & Retrieving Unique Elements
- Python Pandas - Duplicated Labels
- Python Pandas - Grouping & Aggregation
- Python Pandas - GroupBy
- Python Pandas - Time-series Data
- Python Pandas - Date Functionality
- Python Pandas - Timedelta
- Python Pandas - Sparse Data Structures
- Python Pandas - Sparse Data
- Python Pandas - Visualization
- Python Pandas - Visualization
- Python Pandas - Additional Concepts
- Python Pandas - Caveats & Gotchas
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.