Excel to Python: TRIM Function - A Complete Guide | Mito
Home Icon
divider

Functions

divider

Text

divider

TRIM

How to Use Excel's TRIM Function in Pandas

Excel's TRIM function is widely used for removing all spaces from text except for single spaces between words. It's a crucial (and too often forgotten) step in cleaning and preparing data.

This page demonstrates how to achieve similar functionality in Python using pandas, enhancing data cleaning processes for automation.

Mito is an open source library that lets you write Excel formulas in Python. Either write the formula directly in Python or use the TRIM formula in the Mito Spreadsheet and generate the equivalent Python code automatically.

Mito's TRIM function works exactly like it does in Excel. That means you don't need worry about managing data types, handling errors, or the edge case differences between Excel and Python formulas.

Install Mito to start using Excel formulas in Python.

# Import the mitosheet Excel functions
from mitosheet.public.v3 import *;

# Use Mito's TRIM function
# Note: No need to create a regular expression because
# Mito's TRIM function handles stripping whitespaces automatically
df['Column'] = TRIM(df['Column'])
Copy!
Clipboard

In pandas, the equivalent of Excel's TRIM function is the `str.strip` method, which removes leading and trailing whitespace from a string. To replicate Excel's TRIM functionality that also condenses internal spaces, a combination of `str.replace` and `str.strip` can be used.

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.

To remove all spaces from a text string except for single spaces between words, you can combine `str.strip` with `str.replace`. This will ensure that your data is cleanly formatted, similar to how it would be after using Excel's TRIM function.

# Remove leading and trailing spaces
df['Column'] = df['Column'].str.strip()

# Replace remaining multi-space whitespaces with single spaces
df['Column'] = df['Column'].str.replace(r'\s+', ' ', regex=True)
Copy!
Clipboard

If you want to remove all spaces from a text string, you can use `str.replace` with a regular expression to replace all whitespace characters with an empty string.

# Remove all spaces
df['Column'] = df['Column'].str.replace(r'\s+', '', regex=True)
Copy!
Clipboard

To remove whitespace characters from the front of a string, you can use `str.lstrip`. Doing so will remove all whitespace characters from the front of the string, including tabs and new lines, but leave white space characters elsewhere in the string untouched.

# Remove whitespace characters from the front of a string
df['Column'] = df['Column'].str.lstrip()
Copy!
Clipboard

To remove whitespace characters from the end of a string, you can use `str.rstrip`. Doing so will remove all whitespace characters from the end of the string, including tabs and new lines, but leave white space characters elsewhere in the string untouched.

# Remove whitespace characters from the end of a string
df['Column'] = df['Column'].str.rstrip()
Copy!
Clipboard

If you want to remove whitespace characters from the front and end of the string, but leave internal white space characters untouched, you can use `str.strip`.

# Remove leading and trailing whitespace characters
df['Column'] = df['Column'].str.strip()
Copy!
Clipboard

While using pandas to trim strings, there are several pitfalls that can lead to incorrect data cleaning. Here are some common mistakes to watch out for.

When using `str.strip`, it's important to remember that it only removes standard spaces. If your text contains other whitespace characters like tabs or new lines, you'll need to handle them separately.

# Remove standard spaces
df['Column'] = df['Column'].str.strip()

# Remove tabs and new lines as well
df['Column'] = df['Column'].str.replace(r'\t|\n|\r', '', regex=True)
Copy!
Clipboard

Pandas' string methods will return `NaN` when the input value is `NaN`. If that is not the behavior you want, before attempting to trim strings, ensure that you have handled `NaN` values appropriately, either by filling them in or by removing the rows that contain them.

# Fill NaN values with empty strings before trimming
df['Column'] = df['Column'].fillna('')

# Now it's safe to trim
df['Column'] = df['Column'].str.strip()
Copy!
Clipboard

In Excel, the TRIM function is used to remove extra spaces from text, leaving only single spaces between words and no space characters at the start or end of the text.

=TRIM(text)

TRIM Excel Syntax

ParameterDescriptionData Type
textThe text from which you want to remove spaces.string

Examples

FormulaDescriptionResult
=TRIM(" Hello World ")Removes all spaces from the text except for single spaces between words."Hello World"
=TRIM(A1)Removes all the spaces from the text in cell A1.Text without leading or trailing spaces and with only single spaces between words.

Don't re-invent the wheel. Use Excel formulas in Python.

Install Mito

Don't want to re-implement Excel's functionality in Python?

Automate analysis with Mito