How to Use str.find in pandas for String Search
In pandas, use
Series.str.find(substring) to find the position of a substring in each string of a Series. It returns the lowest index where the substring is found or -1 if not found.Syntax
The str.find() method is used on a pandas Series containing strings. It searches for a substring and returns the lowest index of its occurrence.
substring: The string you want to find.- Returns: Integer position of the substring or -1 if not found.
python
Series.str.find(substring)Example
This example shows how to find the position of the substring 'cat' in each string of a pandas Series. If 'cat' is not found, it returns -1.
python
import pandas as pd # Create a Series of strings animals = pd.Series(['cat', 'dog', 'caterpillar', 'bird']) # Find the position of substring 'cat' positions = animals.str.find('cat') print(positions)
Output
0 0
1 -1
2 0
3 -1
dtype: int64
Common Pitfalls
One common mistake is expecting str.find() to return a boolean indicating presence. Instead, it returns the index or -1. To check presence, compare the result to -1.
Also, str.find() works only on string data; non-string values may cause errors or unexpected results.
python
import pandas as pd # Wrong way: expecting boolean animals = pd.Series(['cat', 'dog', 'caterpillar', 'bird']) result_wrong = animals.str.find('cat') print(result_wrong == True) # Incorrect check # Right way: check if found (index >= 0) result_right = animals.str.find('cat') >= 0 print(result_right)
Output
0 False
1 False
2 False
3 False
dtype: bool
0 True
1 False
2 True
3 False
dtype: bool
Quick Reference
| Method | Description | Return Type |
|---|---|---|
| str.find(substring) | Find lowest index of substring | int (index or -1) |
| str.contains(substring) | Check if substring exists | bool |
| str.index(substring) | Find lowest index, error if not found | int |
Key Takeaways
Use Series.str.find(substring) to get the index of a substring or -1 if missing.
Check if substring exists by comparing find result to -1, not by boolean conversion.
str.find works only on string data; non-string values may cause errors.
For boolean presence, consider using Series.str.contains instead.
str.find returns the first occurrence index, useful for locating substrings.