I think need check if values are strings, because mixed values in column - numeric with strings and for each string call strip:

df = df.map(lambda x: x.strip() if isinstance(x, str) else x)
print (df)
                     A    B     C
0                  A b    2   3.0
1                  NaN    2   3.0
2               random   43   4.0
3  any txt is possible  2 1  22.0
4                        23  99.0
5                 help   23   NaN

If columns have same dtypes, not get NaNs like in your sample for numeric values in column B:

cols = df.select_dtypes(['object']).columns
df[cols] = df[cols].apply(lambda x: x.str.strip())
print (df)
                     A    B     C
0                  A b  NaN   3.0
1                  NaN  NaN   3.0
2               random  NaN   4.0
3  any txt is possible  2 1  22.0
4                       NaN  99.0
5                 help  NaN   NaN

(original answer used applymap which is depreciated)

Answer from jezrael on Stack Overflow
🌐
w3resource
w3resource.com › python-exercises › pandas › pandas-remove-leading-and-trailing-whitespace.php
Pandas - Remove leading and trailing whitespace
September 10, 2025 - This exercise shows how to clean text data by removing leading and trailing whitespace using str.strip(). ... import pandas as pd # Create a sample DataFrame with extra whitespace df = pd.DataFrame({ 'Name': [' Artair ', ' Pompiliu ', ' Gerry '] }) # Remove leading and trailing whitespace df['Name_Cleaned'] = df['Name'].str.strip() # Output the result print(df)
🌐
TutorialsPoint
tutorialspoint.com › python-pandas-remove-leading-and-trailing-whitespace-from-more-than-one-column
Python Pandas – Remove leading and trailing whitespace from more than one column
September 15, 2021 - To remove leading or trailing whitespace, use the strip() method. At first, create a DataFrame with 3 columns “Product Category”, “Product Name” and “Quantity” −
🌐
Saturn Cloud
saturncloud.io › blog › how-to-remove-space-from-columns-in-pandas-a-data-scientists-guide
How to Remove Space from Columns in Pandas A Data Scientists Guide | Saturn Cloud Blog
May 1, 2026 - There are different methods you can use depending on your needs. Here are three common approaches: The str.strip() method removes leading and trailing whitespace from strings in a pandas series or dataframe.
🌐
Trymito
trymito.io › excel-to-python › functions › text › TRIM
Excel to Python: TRIM Function - A Complete Guide | Mito
# Remove whitespace characters from the end of a string df['Column'] = df['Column'].str.rstrip() ... If you want to remove whitespace characters from the front and end of the string, but leave internal white space characters untouched, you can ...
🌐
Saturn Cloud
saturncloud.io › blog › how-to-strip-white-space-from-pandas-dataframes
How to Strip White Space from Pandas DataFrames | Saturn Cloud Blog
May 1, 2026 - In this example, we create a DataFrame with two columns, 'A' and 'B', each containing strings with leading and trailing white space. We then apply the str.strip() method to all strings in column 'A' using the .apply() method and a lambda function.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.str.strip.html
pandas.Series.str.strip — pandas 3.0.5 documentation - PyData |
Remove leading and trailing characters · Strip whitespaces (including newlines) or a set of specified characters from each string in the Series/Index from left and right sides. Replaces any non-strings in Series with NaNs. Equivalent to str.strip()
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › pandas-strip-whitespace-from-entire-dataframe
Pandas - Strip whitespace from Entire DataFrame - GeeksforGeeks
January 2, 2025 - It also includes a function called whitespace_remover that iterates over the columns of a given DataFrame, checks if the data type is 'object', and applies the strip function to remove leading and trailing whitespaces.
🌐
APXML
apxml.com › courses › intro-data-cleaning-preprocessing › chapter-5-basic-data-formatting › removing-whitespace
Trim Whitespace from String Data
In most data cleaning scenarios, str.strip() is the preferred method as it handles both leading and trailing whitespace simultaneously. Use lstrip or rstrip only if you have a specific reason to preserve whitespace on one side of the string. This technique is typically applied to columns containing ...
🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to remove leading and trailing whitespace in python pandas
5 Best Ways to Remove Leading and Trailing Whitespace in Python Pandas - Be on the Right Side of Change
March 5, 2024 - The strip() method in Pandas can be applied to a Series to remove leading and trailing whitespace from the strings. Applying strip() to multiple columns can be achieved using the apply() method, which allows the function to be applied column-wise ...
🌐
DataScientYst
datascientyst.com › trim-leading-trailing-white-space-pandas-dataframe
Trim Leading & Trailing White Space in Pandas DataFrame
November 24, 2023 - 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', ...
🌐
sqlpey
sqlpey.com › python › top-5-methods-remove-replace-spaces
Top 5 Methods to Remove or Replace Spaces in DataFrame Column Names
November 24, 2024 - For a performance boost, consider ... ', '_') for col in df.columns] print(df.columns) You can effectively strip leading and trailing spaces using the map() function, which can be faster on large datasets:...
🌐
Vultr Docs
docs.vultr.com › python › third-party › pandas › Series › str › strip
Python Pandas Series str strip() - Remove Leading/Trailing Spaces | Vultr Docs
December 26, 2024 - The strip() method in pandas is part of the string methods under pandas Series str attribute. It’s specifically designed to handle string operations for series data efficiently.
🌐
Finxter
blog.finxter.com › 5-best-ways-to-remove-initial-spaces-from-a-pandas-dataframe
5 Best Ways to Remove Initial Spaces from a Pandas DataFrame – Be on the Right Side of Change
When combined with the apply() method in Pandas, it can remove spaces from the beginning and end of each string in a DataFrame column. ... import pandas as pd df = pd.DataFrame({'A': [' foo', ' bar ', ' baz']}) df['A'] = df['A'].apply(lambda x: x.strip()) print(df) ...
🌐
Arab Psychology
scales.arabpsychology.com › home › how to strip whitespace from columns in pandas
How To Strip Whitespace From Columns In Pandas
November 25, 2025 - The core mechanism involves accessing the string accessor (.str) of a Series (which represents a column in a DataFrame) and applying the strip() function. This operation returns a new Series where leading and trailing whitespace has been ...
🌐
Bobby Hadz
bobbyhadz.com › blog › strip-whitespace-from-headers-in-pandas-dataframe
Pandas: Strip whitespace from Column Headers in DataFrame | bobbyhadz
April 12, 2024 - We used square brackets to access specific columns that contain string values and then called the str.strip() method to strip the leading and trailing whitespace. Make sure the column's values are of type str, otherwise, you'd get the error: ...
🌐
Finxter
blog.finxter.com › 5-best-ways-to-strip-whitespace-from-a-pandas-dataframe-in-python
5 Best Ways to Strip Whitespace from a Pandas DataFrame in Python – Be on the Right Side of Change
March 5, 2024 - This code snippet demonstrates how to use str.strip() on a single column named ‘Column1’. By assigning the stripped column back to the original DataFrame column, we replace the old values with the trimmed versions without leading and trailing whitespace. The DataFrame.applymap() method applies a function elementwise across the entire DataFrame. When paired with str.strip(), it can be used to strip spaces from strings in each cell of the DataFrame.
🌐
Codepointtech
codepointtech.com › home › how to strip whitespace from columns in pandas: a complete guide
How to Strip Whitespace from Columns in Pandas: A Complete Guide - codepointtech.com
July 4, 2026 - Original DataFrame: Product Category Price 0 Laptop Electronics 1200 1 Monitor Electronics 300 2 Keyboard Peripherals 75 3 Mouse Peripherals 25 · The most common and straightforward method is to use .str.strip() on a specific column. This will remove both leading and trailing whitespace.
🌐
Medium
medium.com › @amit25173 › how-to-trim-strings-in-pandas-ac28ca72851b
How to Trim Strings in Pandas. Step-by-Step with Code | by Amit Yadav | Medium
March 6, 2025 - Using str.strip() to Trim Whitespace (Leading & Trailing Spaces) Here’s the superstar of trimming: str.strip(). It removes spaces from both ends of a string. import pandas as pd # Sample DataFrame with messy spaces data = {'Name': [' Alice ...