String Methods—Find, Extract, and Replace
Explore the pandas string methods for finding, extracting, and replacing strings.
Introduction
We continue the exploration of commonly used string methods by looking at the methods for finding, extracting, and replacing text. As before, we’ll use the mock customer dataset from an e-commerce platform.
Preview of Mock E-Commerce Customer Dataset
customer_id | title | first_name | last_name | ip_address | |
264-42-4576 | Mr | gino | Crowdson | 82.48.134.48/5 | gcrowdson0@tamu.edu |
165-49-2539 | Ms | hailey | kirsche | 61.122.97.13/13 | ekirsche1@rambler.ru |
763-23-7634 | Dr | Viviyan | Peschet | 253.140.11.162/2 | rpeschet@ning.com |
Note that the columns in the DataFrame for this dataset have already been converted into StringDtype
.
Find
The find()
method in pandas
is used to search for a substring within each element of a DataFrame column and return the starting index of its first occurrence. Let’s say we want to search for the string 76
in the customer_id
column with the code below:
# Find lowest index of substring occurrencedf['find'] = df['customer_id'].str.find('76')# View outputprint(df[['customer_id', 'find']])
The output above shows the index positions where the substring 76
first occurs:
For the string value
(264-42-45
76
)
in the first row, the substring is first found at the index9
. ...
Get hands-on with 1400+ tech skills courses.