To trim leading and trailing whitespaces from strings in Pandas DataFrame, you can use the str.strip() function to trim leading and trailing whitespaces from strings. Here's an example:

import pandas as pd

data = {'col_1': ['  Apple  ', ' Banana', 'Orange  ', '   Grape   '],
        'col_2': ['   Red ', 'Yellow   ', '  Orange', 'Purple   ']}

df = pd.DataFrame(data)

sample data is:

col_1 col_2
0 Apple Red
1 Banana Yellow
2 Orange Orange
3 Grape Purple

Browsers usually hide extract spaces at start and end - so you can find row DataFrame data below:

         col_1      col_2
0      Apple         Red 
1       Banana  Yellow   
2     Orange       Orange
3     Grape     Purple   

1. remove spaces - whole DataFrame

To remove leading and trailing whitespaces in all columns we can:

df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)

result:

    col_1   col_2
0   Apple     Red
1  Banana  Yellow
2  Orange  Orange
3   Grape  Purple

In this example, the applymap() function is used to apply the strip() method to each element in the DataFrame. The lambda function checks if the element is a string (isinstance(x, str)) before applying the strip method to avoid errors for non-string elements.

After running this code, you'll get a DataFrame where leading and trailing whitespaces in all string columns have been removed.

2. strip spaces in single column

To remove trailing and leading spaces from a single column in Pandas DataFrame we can use:

  • Series method strip()
  • map + strip():
df['col_1'].str.strip()
df['col_1'].map(lambda x: x.strip() if isinstance(x, str) else x)

or

output:

0     Apple
1    Banana
2    Orange
3     Grape
Name: col_1, dtype: object

3. trim trailing spaces with regex

As alternative solution we an use method replace() to remove leading and trailing whitespaces.

df.replace(r"^ +| +$", r"", regex=True)

result:

    col_1   col_2
0   Apple     Red
1  Banana  Yellow
2  Orange  Orange
3   Grape  Purple

lstrip + rstrip

Pandas offers to handy methods for removing extra spaces: rstrip and lstrip:

s.str.rtrip()
s.str.ltrip()

Resources