How to Use str.strip in pandas to Clean Text Data
Use
str.strip() in pandas to remove leading and trailing whitespace or specified characters from string columns. It works on Series or DataFrame columns with string data by calling df['column'].str.strip().Syntax
The basic syntax of str.strip() in pandas is:
Series.str.strip(to_strip=None)
where:
to_strip(optional) is a string of characters to remove from both ends of each string. IfNone, it removes whitespace.
python
df['column'].str.strip() df['column'].str.strip('chars')
Example
This example shows how to remove spaces and specific characters from a pandas DataFrame column using str.strip().
python
import pandas as pd data = {'name': [' Alice ', ' Bob', 'Charlie ', ' David!!']} df = pd.DataFrame(data) # Remove leading and trailing spaces clean_names = df['name'].str.strip() # Remove spaces and exclamation marks clean_names_chars = df['name'].str.strip(' !') print('Original names:') print(df['name']) print('\nAfter str.strip():') print(clean_names) print('\nAfter str.strip(" !"):') print(clean_names_chars)
Output
Original names:
0 Alice
1 Bob
2 Charlie
3 David!!
Name: name, dtype: object
After str.strip():
0 Alice
1 Bob
2 Charlie
3 David!!
Name: name, dtype: object
After str.strip(" !"):
0 Alice
1 Bob
2 Charlie
3 David
Name: name, dtype: object
Common Pitfalls
Common mistakes when using str.strip() include:
- Expecting it to remove characters inside the string, but it only removes from the start and end.
- Not specifying
to_stripwhen you want to remove characters other than whitespace. - Using
strip()on non-string columns without converting them first, which causes errors.
python
import pandas as pd data = {'col': [' hello ', 'world!!', 123]} df = pd.DataFrame(data) # Wrong: applying str.strip() on non-string values causes error # df['col'].str.strip() # This will raise an error # Right: convert to string first clean_col = df['col'].astype(str).str.strip(' !') print(clean_col)
Output
0 hello
1 world
2 123
Name: col, dtype: object
Quick Reference
| Method | Description | Example |
|---|---|---|
| str.strip() | Removes leading and trailing whitespace | df['col'].str.strip() |
| str.strip(chars) | Removes specified characters from both ends | df['col'].str.strip(' !') |
| astype(str).str.strip() | Convert non-string to string then strip | df['col'].astype(str).str.strip() |
Key Takeaways
Use df['column'].str.strip() to remove spaces from start and end of strings.
Specify characters in str.strip('chars') to remove them instead of whitespace.
str.strip() only removes characters from the ends, not inside the string.
Convert non-string columns to string before using str.strip() to avoid errors.
str.strip() works on pandas Series or DataFrame columns with string data.