We check for missing or repeated data to keep our data clean and reliable.
When you want to find missing values in a customer list.
Before analyzing sales data, to remove repeated entries.
To check if survey responses have empty answers.
When preparing data for machine learning to avoid errors.
To ensure data quality before reporting results.
Syntax
Apache Spark
df.filter(df['column'].isNull())
df.dropDuplicates()
# To count nulls in a column:
df.filter(df['column'].isNull()).count()
# To find duplicate rows:
df.groupBy(df.columns).count().filter('count > 1')
isNull() checks for missing values in a column.
dropDuplicates() removes repeated rows from the DataFrame.
Examples
Shows rows where the 'age' column has missing values.
Apache Spark
df.filter(df['age'].isNull()).show()
Removes duplicate rows and shows the unique rows.
Apache Spark
df.dropDuplicates().show()
Finds and shows rows that appear more than once (duplicates).
This program creates a small table with some missing ages and repeated rows. It shows how to find missing ages, detect duplicates, and remove duplicates.
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('NullDuplicateDemo').getOrCreate()
# Sample data with nulls and duplicates
data = [
(1, 'Alice', 25),
(2, 'Bob', None),
(3, 'Charlie', 30),
(4, 'Bob', None),
(5, None, 22),
(1, 'Alice', 25)
]
columns = ['id', 'name', 'age']
df = spark.createDataFrame(data, columns)
print('Original DataFrame:')
df.show()
print('Rows with nulls in age column:')
df.filter(df['age'].isNull()).show()
print('Duplicate rows:')
df.groupBy(df.columns).count().filter('count > 1').show()
print('DataFrame after dropping duplicates:')
df.dropDuplicates().show()
spark.stop()
OutputSuccess
Important Notes
Null values can cause errors in calculations if not handled.
Duplicates can bias your analysis by counting the same data multiple times.
Always check for nulls and duplicates before deeper analysis.
Summary
Null detection finds missing data in columns.
Duplicate detection finds repeated rows.
Cleaning data improves analysis accuracy.
Practice
(1/5)
1. What does null detection in Apache Spark help you find in a dataset?
easy
A. Columns with only numeric data
B. Rows with duplicate values
C. Missing or empty values in columns
D. The total number of rows
Solution
Step 1: Understand null detection purpose
Null detection identifies missing or empty values in dataset columns.
Step 2: Differentiate from duplicates
Duplicate detection finds repeated rows, not missing values.
Final Answer:
Missing or empty values in columns -> Option C
Quick Check:
Null detection = Missing values [OK]
Hint: Null detection finds missing data, not duplicates [OK]
Common Mistakes:
Confusing null detection with duplicate detection
Thinking null means zero or empty string
Assuming null detection counts rows
2. Which Apache Spark DataFrame method is used to find duplicate rows?
easy
A. count()
B. isNull()
C. fillna()
D. dropDuplicates()
Solution
Step 1: Identify method for duplicates
The method dropDuplicates() removes duplicate rows from a DataFrame.
Step 2: Confirm other methods' purposes
isNull() checks for nulls, fillna() fills nulls, count() counts rows.
Final Answer:
dropDuplicates() -> Option D
Quick Check:
Duplicates removed by dropDuplicates() [OK]
Hint: Use dropDuplicates() to find/remove duplicates [OK]
Common Mistakes:
Using isNull() to find duplicates
Confusing fillna() with duplicate removal
Trying count() to detect duplicates
3. Given this Spark code snippet, what is the output?
A. dropDuplicates() removes duplicates, so count shows unique rows, not duplicates
B. The code will raise a syntax error
C. dropDuplicates() counts duplicates instead of removing them
D. The DataFrame is empty, so count is zero
Solution
Step 1: Understand dropDuplicates() behavior
dropDuplicates() removes duplicate rows, so count() returns unique row count.
Step 2: Identify mistake in duplicate detection
Counting duplicates requires comparing total count with unique count, not just unique count alone.
Final Answer:
dropDuplicates() removes duplicates, so count shows unique rows, not duplicates -> Option A
Quick Check:
dropDuplicates() = unique rows, not duplicates count [OK]
Hint: Compare total count and dropDuplicates count to find duplicates [OK]
Common Mistakes:
Assuming dropDuplicates() counts duplicates
Expecting syntax error from correct code
Ignoring difference between total and unique counts
5. You have a Spark DataFrame with columns 'user_id' and 'email'. You want to remove rows where 'email' is null and also remove duplicate rows based on 'user_id'. Which code snippet correctly does this?
hard
A. df.filter(df['email'].isNotNull()).dropDuplicates(['user_id'])
B. df.dropDuplicates(['user_id']).filter(df['email'].isNull())
C. df.filter(df['email'].isNull()).dropDuplicates(['email'])
D. df.dropDuplicates().filter(df['email'].isNotNull())
Solution
Step 1: Filter out rows with null emails
Use filter(df['email'].isNotNull()) to keep only rows with email values.
Step 2: Remove duplicates by 'user_id'
Use dropDuplicates(['user_id']) to remove rows with repeated user_id values.
Final Answer:
df.filter(df['email'].isNotNull()).dropDuplicates(['user_id']) -> Option A
Quick Check:
Filter null emails then drop duplicates by user_id [OK]
Hint: Filter nulls first, then drop duplicates by key column [OK]
Common Mistakes:
Filtering null emails after dropping duplicates
Dropping duplicates on wrong column
Filtering for null emails instead of non-null
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.