String Methods—Basic Transformations and Checks
Learn how to use string methods to perform basic transformations and checks on string values.
Introduction
In this lesson, we’ll explore the many capabilities and methods for working with string data type values in pandas DataFrames. We’ll first do a quick refresher of the basic transformation and checks of string values based on the following 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 |
The pandas DataFrame for the dataset can be viewed below:
# Convert all DataFrame columns to string typedf = df.astype('string')# View DataFrame of mock customer datasetprint(df)print('=' * 40)print(df.dtypes)
String accessors
Given that pandas DataFrames are a collection of Series objects, string values in these columns can be accessed via the str accessor. The str accessor provides a set of vectorized string methods that can be used to manipulate strings in a DataFrame. These string methods generally have names that match the ...