Pandas: How to Use lstrip() and rstrip() Functions


Often you may want to remove specific leading or trailing characters from strings in a pandas DataFrame.

The easiest way to do so is by using the following functions in pandas:

  • Series.str.lstrip(): Remove leading characters from string
  • Series.str.rstrip(): Remove trailing characters from string

Note that both of these functions can be used to remove specific leading or trailing characters from a string, but if you don’t provide one or more specific characters to remove then the default behavior is to remove whitespaces from the string.

The following examples show how to use both the lstrip() and rstrip() functions in practice with a pandas DataFrame.

Example 1: How to Use the lstrip() Function in Pandas

Suppose we create the following pandas DataFrame that contains information about employee ID numbers along with their total sales made during a given year:

import pandas as pd

#create pandas DataFrame
df = pd.DataFrame({'employee': ['A001', 'A002', 'A003', 'A004', 'A005', 'A006'],
                   'sales': [130, 158, 203, 188, 205, 178]})

#view pandas DataFrame
print(df)

  employee  sales
0     A001    130
1     A002    158
2     A003    203
3     A004    188
4     A005    205
5     A006    178

Suppose that we would like to strip any leading ‘A’ values from each employee ID.

We can use the lstrip() function with the following syntax to do so:

#strip leading 'A' values from each string in 'employee' column
df['employee'].str.lstrip('A')

0    001
1    002
2    003
3    004
4    005
5    006
Name: employee, dtype: object

Notice that all ‘A’ characters in each string of the ’employee’ column have been stripped away.

If you would like to specify multiple leading characters to strip from the string then you can use the | operator as follows:

#strip leading 'A' or '0' values from each string in 'employee' column
df['employee'].str.lstrip('A|0')

0    1
1    2
2    3
3    4
4    5
5    6
Name: employee, dtype: object

Notice that all ‘A’ and ‘0’ characters in each string of the ’employee’ column have been stripped away.

Feel free to include as many | operators as you would like to specify multiple specific leading characters to remove from each string.

Note: You can find the complete documentation for the lstrip() function in pandas here.

Example 2: How to Use the rstrip() Function in Pandas

Suppose we create the following pandas DataFrame that contains information about employee ID numbers along with their total sales made during a given year:

import pandas as pd

#create pandas DataFrame
df = pd.DataFrame({'employee': ['A001AA', 'A002A', 'A003', 'A004AA', 'A005', 'A006'],
                   'sales': [130, 158, 203, 188, 205, 178]})

#view pandas DataFrame
print(df)

  employee  sales
0   A001AA    130
1    A002A    158
2     A003    203
3   A004AA    188
4     A005    205
5     A006    178

Suppose that we would like to strip any trailing ‘A’ values from each employee ID.

We can use the rstrip() function with the following syntax to do so:

#strip trailing 'A' values from each string in 'employee' column
df['employee'].str.rstrip('A')

0    A001
1    A002
2    A003
3    A004
4    A005
5    A006
Name: employee, dtype: object

Notice that all trailing ‘A’ characters in each string of the ’employee’ column have been stripped away.

Also note that you can use the | operator to specify multiple trailing characters to strip from each string.

Note: You can find the complete documentation for the rstrip() function in pandas here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Use asfreq() Function in Pandas
How to Use the nunique() Function in Pandas
How to Use the get_loc() Function in Pandas
How to Use idxmin() Function in Pandas

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *