0
0
Pandasdata~15 mins

str.strip() for whitespace in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Clean Up Product Names Using str.strip()
📖 Scenario: You work in a store's data team. The product names in the sales data have extra spaces at the start or end. This makes it hard to analyze the data correctly.
🎯 Goal: You will clean the product names by removing extra spaces using str.strip() in pandas.
📋 What You'll Learn
Create a pandas DataFrame with product names including extra spaces
Create a variable to hold the column name 'Product'
Use str.strip() on the product names to remove spaces
Print the cleaned product names column
💡 Why This Matters
🌍 Real World
Cleaning text data is a common task in data science to prepare data for analysis or machine learning. Extra spaces can cause errors or wrong results.
💼 Career
Data scientists and analysts often clean messy data like product names, customer info, or survey answers before working with it.
Progress0 / 4 steps
1
Create the sales DataFrame with product names
Create a pandas DataFrame called sales with one column named 'Product'. The column should have these exact values with spaces: ' Apple', 'Banana ', ' Cherry ', 'Date'.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary. The key is 'Product' and the value is the list of product names with spaces.

2
Create a variable for the product column name
Create a variable called product_col and set it to the string 'Product'.
Pandas
Need a hint?

Just assign the string 'Product' to the variable product_col.

3
Remove extra spaces from product names using str.strip()
Use str.strip() on the sales[product_col] column to remove spaces at the start and end. Save the cleaned names back to sales[product_col].
Pandas
Need a hint?

Use sales[product_col].str.strip() to remove spaces and assign it back to sales[product_col].

4
Print the cleaned product names
Print the sales[product_col] column to show the cleaned product names without extra spaces.
Pandas
Need a hint?

Use print(sales[product_col]) to display the cleaned product names.