📖 Scenario: You have a small dataset of sales records. Some records have missing values. You want to clean the data by removing any rows that have missing values.
🎯 Goal: Build a small pandas DataFrame with sales data, then use dropna() to remove rows with missing values, and finally display the cleaned data.
📋 What You'll Learn
Create a pandas DataFrame called sales_data with exact columns and values
Create a variable called cleaned_data that holds the DataFrame after dropping rows with missing values using dropna()
Print the cleaned_data DataFrame
💡 Why This Matters
🌍 Real World
Cleaning data by removing incomplete records is a common step before analysis or machine learning.
💼 Career
Data scientists and analysts often use <code>dropna()</code> to prepare datasets for accurate insights and models.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact columns and values: 'Product': ['Pen', 'Notebook', 'Pencil', 'Eraser', 'Marker'], 'Price': [1.5, 2.0, None, 0.5, 1.0], 'Quantity': [10, None, 15, 20, 10]. Use None for missing values.
Pandas
Hint
Use pd.DataFrame() with a dictionary. Use None for missing values.
2
Create a variable for cleaned data
Create a variable called cleaned_data that stores the result of calling dropna() on sales_data to remove rows with any missing values.
Pandas
Hint
Use dropna() on sales_data and assign it to cleaned_data.
3
Check the cleaned data
Use print() to display the cleaned_data DataFrame.
Pandas
Hint
Use print(cleaned_data) to see the cleaned DataFrame.
4
View the final cleaned data output
Run the program to see the printed cleaned_data DataFrame without any missing values.
Pandas
Hint
The output should show only rows without any missing values.
Practice
(1/5)
1. What does the dropna() function do in pandas?
easy
A. Sorts the DataFrame by index
B. Fills missing values with zeros
C. Removes rows or columns with missing values
D. Calculates the mean of columns
Solution
Step 1: Understand the purpose of dropna()
The dropna() function is designed to remove data that contains missing values (NaN) from a DataFrame.
Step 2: Identify what dropna() removes
It removes either rows or columns that have missing values, depending on the parameters used.
Final Answer:
Removes rows or columns with missing values -> Option C
Quick Check:
dropna() removes missing data [OK]
Hint: Remember: dropna() drops missing data, not fills it [OK]
Common Mistakes:
Confusing dropna() with fillna()
Thinking dropna() sorts data
Assuming dropna() calculates statistics
2. Which of the following is the correct syntax to drop rows with any missing values in a DataFrame df?
easy
A. df.dropna(axis=0, how='any')
B. df.dropna(axis=1, how='any')
C. df.dropna(axis=0, how='all')
D. df.dropna(axis=1, how='all')
Solution
Step 1: Identify axis for rows
In pandas, axis=0 means rows, so to drop rows, use axis=0.
Step 2: Use how='any' to drop rows with any missing values
The how='any' parameter drops rows if any value is missing in that row.
Final Answer:
df.dropna(axis=0, how='any') -> Option A
Quick Check:
Drop rows with any NaN = axis=0, how='any' [OK]
Hint: axis=0 drops rows; how='any' drops if any NaN [OK]
Only row 0 has no missing values, so only it remains after dropna(axis=0, how='any').
Final Answer:
Only row with index 0 -> Option D
Quick Check:
Drop rows with any NaN leaves only complete rows [OK]
Hint: Check which rows have any NaN to keep them [OK]
Common Mistakes:
Keeping rows with NaN
Confusing axis=0 and axis=1
Misreading how='any' vs how='all'
4. You run the code:
df.dropna(axis=2)
and get an error. What is the problem?
medium
A. axis=2 is invalid; only 0 (rows) or 1 (columns) allowed
B. dropna() cannot be used on DataFrames
C. Missing values must be filled before dropping
D. You must specify how='any' or how='all'
Solution
Step 1: Understand valid axis values for dropna()
In pandas, axis can only be 0 (rows) or 1 (columns) for dropna().
Step 2: Identify the error cause
Using axis=2 is invalid and causes an error because it is not a supported axis.
Final Answer:
axis=2 is invalid; only 0 (rows) or 1 (columns) allowed -> Option A
Quick Check:
Valid axis values = 0 or 1 only [OK]
Hint: axis must be 0 or 1 for dropna() [OK]
Common Mistakes:
Using axis=2 mistakenly
Thinking dropna() needs fillna() first
Assuming how parameter is mandatory
5. You have a DataFrame df with some rows having all values missing and some rows having only some missing values. Which dropna() call will remove only rows where all values are missing, keeping rows with partial data?
hard
A. df.dropna(axis=0, how='any')
B. df.dropna(axis=0, how='all')
C. df.dropna(axis=1, how='any')
D. df.dropna(axis=1, how='all')
Solution
Step 1: Understand how='all' parameter
Using how='all' drops rows only if all values in that row are missing.
Step 2: Use axis=0 to drop rows
Since we want to drop rows, axis=0 is correct.
Step 3: Confirm this keeps rows with some data
Rows with some non-missing values are kept because not all values are missing.
Final Answer:
df.dropna(axis=0, how='all') -> Option B
Quick Check:
Drop rows with all NaN = axis=0, how='all' [OK]
Hint: Use how='all' to drop only fully empty rows [OK]
Common Mistakes:
Using how='any' which drops partial rows
Using axis=1 which drops columns
Confusing how parameter effects
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.