String Methods—Slice, Split, and Partition
Understand how to apply string methods to slice, split, and partition string values.
We'll cover the following...
Introduction
Having covered the string methods for basic transformations and checks, let's move on to the next step by looking at the more advanced transformations we can apply to string values. In particular, we’ll explore the string methods that enable us to slice, split, and partition 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 the StringDtype
.
Slice
Slicing string values in pandas
is a way of selecting a subset of characters from a string column. It allows us to extract a portion of a string based on the position of its characters. There are two ways to do slicing:
Index brackets
[]
String method
slice()
For example, if we want to obtain the first three digits of the customer_id
column, we can run the following code:
# Method 1 - Index brackets for slicingdf['slicing_with_index_brackets'] = df['customer_id'].str[0:3]# Method 2 - slice() methoddf['slicing_with_slice_method'] = df['customer_id'].str.slice(start=0, stop=3)# View outputprint(df[['customer_id', 'slicing_with_index_brackets', 'slicing_with_slice_method']])
The output above shows that both methods produce the same correct, sliced outcome. We also notice that in both cases, the starting index is inclusive, whereas the ending index is exclusive. It means that for a start value of 0
and a stop value of 3
, the index positions that will be included in the ...