String Methods—Concatenate and Match
Learn about the pandas string methods used for concatenation and pattern matching.
Introduction
Let's continue exploring the commonly used advanced transformations that can be applied to string values. In particular, we’ll dive into the methods that allow us to concatenate and match string values. 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
.
Concatenate
Joining strings is a common operation to perform for string values, and the cat()
string method allows us to do so. For instance, we can concatenate the first_name
and last_name
columns to create a new full_name
column with white space between the names, as shown below:
# Concatenate first and last namesdf['full_name'] = df['first_name'].str.cat(others=df['last_name'],sep=' ')# View outputprint(df[['full_name', 'first_name', 'last_name']])
If null values are present, we can use the na_rep
parameter to specify the string to replace them. The last row in the output below illustrates how the null value in the first_name
column was replaced with another string ('unknown'
) in the ...
Get hands-on with 1400+ tech skills courses.