📖 Scenario: Imagine you work in a small online store. You have a list of customer orders, but the data is messy. Some orders have missing prices, some have wrong product names, and some have extra spaces. To understand your sales, you need to clean this data carefully.
🎯 Goal: You will create a small dataset of orders, set a rule to identify invalid prices, clean the data by fixing or removing bad entries, and finally show the cleaned data. This will teach you why cleaning data step-by-step is important before analysis.
📋 What You'll Learn
Create a pandas DataFrame with given order data
Set a price threshold to identify invalid prices
Use pandas methods to clean the data systematically
Print the cleaned DataFrame as the final output
💡 Why This Matters
🌍 Real World
Cleaning data is a crucial first step in any data analysis or business decision. Messy data can lead to wrong conclusions.
💼 Career
Data scientists and analysts spend a lot of time cleaning data before they can analyze it. This project shows why systematic cleaning is important.
Progress0 / 4 steps
1
Create the initial orders DataFrame
Import pandas as pd and create a DataFrame called orders with these exact columns and rows: OrderID: [101, 102, 103, 104, 105] Product: [' apple', 'banana', 'Orange ', 'banana', 'apple'] Price: [1.2, 0, 0.8, -1, 1.5]
Pandas
Hint
Use pd.DataFrame with a dictionary of lists for columns.
2
Set a price threshold for invalid prices
Create a variable called min_price and set it to 0.5. This will help us find prices that are too low or invalid.
Pandas
Hint
Just create a variable named min_price and assign 0.5.
3
Clean the data by fixing product names and filtering prices
Create a new DataFrame called clean_orders by doing these steps: 1. Remove extra spaces from the Product column using str.strip(). 2. Keep only rows where Price is greater than or equal to min_price.
Pandas
Hint
Use str.strip() on the Product column and filter rows with orders['Price'] >= min_price.
4
Print the cleaned orders DataFrame
Print the clean_orders DataFrame to see the cleaned data.
Pandas
Hint
Use print(clean_orders) to show the cleaned data.
Practice
(1/5)
1. Why is systematic cleaning important when working with data in pandas?
easy
A. It increases the file size of the dataset.
B. It makes the data look colorful and pretty.
C. It helps ensure the analysis results are accurate and reliable.
D. It automatically creates new data columns.
Solution
Step 1: Understand the purpose of cleaning data
Cleaning removes errors and inconsistencies that can mislead analysis.
Step 2: Connect cleaning to analysis trustworthiness
Systematic cleaning ensures results are based on correct and consistent data.
Final Answer:
It helps ensure the analysis results are accurate and reliable. -> Option C
Quick Check:
Cleaning = Accurate results [OK]
Hint: Cleaning fixes errors to trust your results [OK]
Common Mistakes:
Thinking cleaning only changes data appearance
Believing cleaning adds new data automatically
Assuming cleaning increases data size
2. Which pandas function is commonly used to remove rows with missing values?
easy
A. df.replace()
B. df.dropna()
C. df.drop_duplicates()
D. df.fillna()
Solution
Step 1: Identify function to remove missing data
df.dropna() removes rows with any missing (NaN) values.
Believing drop_duplicates() cannot work on numbers
5. You have a DataFrame with messy text data in a column 'City' that contains extra spaces and inconsistent capitalization. Which sequence of pandas operations best cleans this column systematically?
hard
A. df['City'] = df['City'].str.strip().str.title()
# No further cleaning needed
B. df['City'] = df['City'].str.strip().str.title()
df['City'] = df['City'].str.replace(' ', '')