Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Remove Whitespace from Strings | Foundations of Data Cleaning
Python for Data Cleaning

bookChallenge: Remove Whitespace from Strings

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
Uppgift

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.

Lösning

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

Awesome!

Completion rate improved to 5.56

bookChallenge: Remove Whitespace from Strings

Svep för att visa menyn

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
Uppgift

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.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 5
single

single

some-alt