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