Using str.split().agg("".join)

Ex:

df['Product'] = df['Product'].str.split().agg("".join)
#or
#df['Product'] = df['Product'].str.replace(r"(\s+)", "")
print(df)

Output:

  Product
0   Prod1
1   Prod1
2   Prod2
3   Prod2
4   Prod2
5   Prod3
6   Prod3
Answer from Rakesh on Stack Overflow
🌐
Trymito
trymito.io › excel-to-python › functions › text › TRIM
Excel to Python: TRIM Function - A Complete Guide | Mito
In pandas, you can use the `str.strip` method to remove leading and trailing spaces from strings in a DataFrame column. This mirrors the basic functionality of Excel's TRIM function.
Discussions

How to remove multiple white spaces from data frame
document = docx2python("R/survey.docx") lines = document.text.split('\n') self.content = pd.DataFrame(lines, columns=['text']) self.content = self.content['text'].str.strip() self.content.replace('\s+', '', regex=True, inplace=True) Try that pattern instead. More on reddit.com
🌐 r/learnpython
4
1
March 3, 2023
python - Remove Multiple Blanks In DataFrame - Stack Overflow
How do I remove multiple spaces between two strings in python. e.g:- "Bertug 'here multiple blanks' Mete" => "Bertug Mete" to "Bertug Mete" Input is read from an .xls file. I have tried More on stackoverflow.com
🌐 stackoverflow.com
python - Removing unnecessary spaces in a string column - Stack Overflow
I have a string column in a pandas dataframe such as the following where there are a lot of extra white space characters (leading, in-between other words, trailing). I want to remove all such extra... More on stackoverflow.com
🌐 stackoverflow.com
python - How to remove excess spaces in-between words in dataframe index? - Stack Overflow
The index of my df are strings of company names. Eg Wells Fargo Sometimes there are excess spaces in-between the words I want to convert to only single spaces. I tried the below but got errors. ** More on stackoverflow.com
🌐 stackoverflow.com
January 3, 2022
🌐
Medium
medium.com › @amit25173 › how-to-remove-whitespace-from-strings-in-pandas-bfd9acdc55f3
How to Remove Whitespace from Strings in pandas? | by Amit Yadav | Medium
March 6, 2025 - You might have noticed that some datasets contain extra spaces in multiple columns, like names, addresses, or categories. Manually cleaning each column would be exhausting. ✅ The best approach? Apply strip() to multiple columns at once.
🌐
GeeksforGeeks
geeksforgeeks.org › pandas › pandas-strip-whitespace-from-entire-dataframe
Pandas - Strip whitespace from Entire DataFrame - GeeksforGeeks
January 2, 2025 - Here, we uses the pandas library to read a CSV file named 'student_data.csv' and employs the skipinitialspace=True parameter to eliminate leading spaces in the data while loading it into a DataFrame.
🌐
APXML
apxml.com › courses › intro-data-cleaning-preprocessing › chapter-5-basic-data-formatting › removing-whitespace
Trim Whitespace from String Data
Notice the extra spaces around 'London' (leading), 'Paris' (trailing), and 'Berlin' (both). To clean the 'City' column, we can apply the str.strip() method to it. This method, when called on a pandas Series containing strings, removes whitespace from the beginning and end of each string in ...
🌐
Reddit
reddit.com › r/learnpython › how to remove multiple white spaces from data frame
r/learnpython on Reddit: How to remove multiple white spaces from data frame
March 3, 2023 -

I`m storing a text file as a dataframe, and I cant remove the stubborn white space:

document = docx2python("R/survey.docx")
lines = document.text.split('\n')
self.content = pd.DataFrame(lines, columns=['text'])
self.content = self.content['text'].str.strip()
self.content = self.content.str.replace(r'\s{2,}','',regex=True)

Oddly enough it removes some of the whitespace, but not all of it such as the tabs

🌐
Era-edta
web.era-edta.org › uploads › lglcaox › pandas-remove-extra-spaces-between-words
pandas remove extra spaces between words
To remove white spaces present at start and end of the string, you can use strip() function on the string. def remove (string): return "".join (string.split ()) string = ' g e e k '. However, sometimes there might be empty lines within a text. df["text"] = df["text"].apply(lambda text: re.sub(' ...
Find elsewhere
🌐
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 - # Using replace with regex to remove all excess whitespace df['Column1'].replace(to_replace=r'\s+', value=' ', regex=True, inplace=True) ... The code snippet uses replace() to target all instances of one or more spaces (\s+) in ‘Column1’ and replaces them with a single space. This action both normalizes internal spaces and preserves the legitimate separations between words.
🌐
Studyopedia
studyopedia.com › home › remove whitespace or specific characters in pandas
Remove Whitespace or specific characters in Pandas - Studyopedia
December 22, 2025 - To remove whitespace on text data in a Series or DataFrame, use the strip(), lstrip() and rstrip() methods in Python Pandas.
🌐
w3resource
w3resource.com › python-exercises › pandas › pandas-remove-leading-and-trailing-whitespace.php
Pandas - Remove leading and trailing whitespace
September 10, 2025 - 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) ... Created a DataFrame with text data containing leading and trailing whitespace. Used str.strip() to remove the extra spaces around the 'Name' column values.
🌐
Medium
medium.com › @ricardogr07 › 100-days-of-data-science-day-39-removing-unwanted-characters-from-text-columns-5b226a1f2fce
Day 39 — Removing Unwanted Characters from Text Columns | by Ricardo García Ramírez | Medium
October 9, 2024 - Stripping Whitespace: Remove leading, trailing, or extra spaces between words. Removing Numbers: Strip out numerical characters if they are irrelevant for the analysis. Let’s walk through how to clean text data using Python’s re library ...
🌐
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()
🌐
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 - The str.strip() method removes leading and trailing whitespace from strings in a pandas series or dataframe. You can use this method to remove spaces from column names or column values.
🌐
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 - Q3: How to trim whitespace when reading a CSV file with pandas? Use the skipinitialspace=True parameter while reading the CSV: df = pd.read_csv('file.csv', skipinitialspace=True) This trims spaces right after delimiters. Q4: Is there a performance difference between strip(), lstrip(), and rstrip() for large datasets? Negligible. All are optimized for performance. Use based on your trimming needs. Q5: How do I remove extra spaces between words, not just at the start/end?
🌐
Skytowner
skytowner.com › explore › stripping_whitespace_from_columns_in_pandas
Stripping whitespace from columns in Pandas
To strip whitespace from columns in Pandas we can use the str.strip(~) method or the str.replace(~) method.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.str.strip.html
pandas.Series.str.strip — pandas 2.3.3 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()