Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Remove Whitespace from Strings | Foundations of Data Cleaning
Python for Data Cleaning
Sectionย 1. Chapterย 5
single

single

bookChallenge: Remove Whitespace from Strings

Swipe to show menu

When working with categorical data in a DataFrame, extra whitespace at the beginning or end of string values can cause serious inconsistencies. For example, the values "apple", " apple", and "apple " may look the same to you, but Python treats them as different strings. This can lead to problems when grouping, filtering, or comparing data, and may result in incorrect analysis or missed patterns. Cleaning up these inconsistencies by stripping whitespace is a crucial first step in preparing your data for analysis.

12345678910
import pandas as pd data = { "Fruit": [" apple", "banana ", " cherry ", "date"], "Color": [" red", "yellow ", " red ", "brown"], "Count": [10, 5, 7, 3] } df = pd.DataFrame(data) print(df)
copy
Note
Note

You can use select_dtypes(include="object") to select only the columns in a DataFrame that contain string data. This makes it easy to apply string operations, such as str.strip(), only to columns storing text and not to columns with numbers or other types.

Task

Swipe to start coding

Write a function that removes leading and trailing whitespace from all string columns in a DataFrame.

  • The function must return a new DataFrame with the same columns as the input.
  • All leading and trailing whitespace must be removed from every string value in columns with string data type.
  • Non-string columns must remain unchanged.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 1. Chapterย 5
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt