📖 Scenario: You work as a data analyst for a small retail company. You receive daily sales data in tables. Before using the data for reports, you need to check if the data is clean and valid.For example, sales numbers should not be negative, and product names should not be empty.
🎯 Goal: Build a simple data validation check using pandas to find invalid sales records.You will create a DataFrame, set a validation rule, filter invalid rows, and print them.
📋 What You'll Learn
Create a pandas DataFrame with given sales data
Create a validation threshold variable for minimum valid sales
Use a filter to find rows where sales are below the threshold
Print the invalid rows
💡 Why This Matters
🌍 Real World
Data validation is essential in real-world data analysis to ensure reports and decisions are based on clean and accurate data.
💼 Career
Data analysts and scientists often perform validation checks to catch errors early and maintain data quality.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact entries: {'Product': ['Apples', 'Bananas', 'Cherries', 'Dates'], 'Sales': [50, -10, 30, 0]}
Pandas
Hint
Use pd.DataFrame() with a dictionary containing the product names and sales numbers.
2
Set the minimum valid sales threshold
Create a variable called min_valid_sales and set it to 1 to represent the minimum valid sales number.
Pandas
Hint
This variable will help us check if sales are valid (1 or more).
3
Filter invalid sales records
Create a new DataFrame called invalid_sales by filtering sales_data to include only rows where the Sales value is less than min_valid_sales.
Pandas
Hint
Use boolean indexing with sales_data['Sales'] < min_valid_sales to filter rows.
4
Print the invalid sales records
Print the invalid_sales DataFrame to display the rows with invalid sales.
Pandas
Hint
Use print(invalid_sales) to show the filtered rows.
Practice
(1/5)
1. What does the pandas function isnull() check in a DataFrame?
easy
A. It checks for missing or null values in the data.
B. It checks for duplicate rows in the DataFrame.
C. It returns the data types of each column.
D. It sorts the DataFrame by a column.
Solution
Step 1: Understand the purpose of isnull()
The isnull() function in pandas returns a DataFrame of the same shape with True where values are missing (NaN) and False otherwise.
Step 2: Compare with other functions
Functions like duplicated() check duplicates, and dtypes shows data types, so they do not check for missing values.
Final Answer:
It checks for missing or null values in the data. -> Option A
Quick Check:
Missing data = isnull() [OK]
Hint: Missing values? Use isnull() to find them fast [OK]
Common Mistakes:
Confusing isnull() with duplicated()
Thinking isnull() returns data types
Assuming isnull() sorts data
2. Which of the following is the correct syntax to check for duplicate rows in a pandas DataFrame named df?
easy
A. df.isnull()
B. df.check_duplicates()
C. df.duplicated()
D. df.find_duplicates()
Solution
Step 1: Recall pandas method for duplicates
The correct pandas method to find duplicate rows is duplicated(), called as df.duplicated().
Step 2: Verify other options
isnull() checks missing data, and check_duplicates() and find_duplicates() are not valid pandas methods.
Final Answer:
df.duplicated() -> Option C
Quick Check:
Duplicates = duplicated() [OK]
Hint: Duplicates? Use df.duplicated() to spot them [OK]
Common Mistakes:
Using non-existent methods like check_duplicates()
Mixing isnull() with duplicate checks
Forgetting parentheses after duplicated
3. Given the DataFrame df below, what will df.isnull().sum() output?
Column 'A' has one None (missing), 'B' has one None, and 'C' has no missing values.
Step 2: Understand what isnull().sum() does
isnull() marks missing as True, and sum() counts True values per column.
Final Answer:
{'A': 1, 'B': 1, 'C': 0} -> Option D
Quick Check:
Count missing per column = isnull().sum() [OK]
Hint: Sum isnull() to count missing per column [OK]
Common Mistakes:
Counting total missing instead of per column
Confusing None with zero
Misreading DataFrame values
4. The following code is intended to find duplicate rows in df. What is the error?
duplicates = df.duplicate()
medium
A. The method name should be duplicated(), not duplicate().
B. The DataFrame df is not defined.
C. The method duplicated() requires an argument.
D. The code should use df.isnull() to find duplicates.
Solution
Step 1: Check method name correctness
The correct pandas method to find duplicates is duplicated(), not duplicate().
Step 2: Verify other options
df is assumed defined, duplicated() does not require arguments, and isnull() checks missing data, not duplicates.
Final Answer:
The method name should be duplicated(), not duplicate(). -> Option A
Quick Check:
Correct method name = duplicated() [OK]
Hint: Remember: duplicated() has a 'd' at the end [OK]
Common Mistakes:
Misspelling duplicated() as duplicate()
Confusing duplicated() with isnull()
Thinking duplicated() needs arguments
5. You have a DataFrame df with columns ['id', 'age', 'score']. You want to validate that age has no missing values and score has no duplicates. Which code correctly performs both checks?