What is the purpose of detecting null values in a dataset?
Detecting null values helps identify missing or incomplete data, which can affect analysis and model accuracy. Handling nulls properly ensures cleaner, more reliable results.
Click to reveal answer
beginner
How do you check for null values in a Spark DataFrame?
You can use the filter or where method with isNull() on a column, for example: df.filter(df['column'].isNull()).show().
Click to reveal answer
intermediate
What is the difference between dropDuplicates() and distinct() in Spark?
dropDuplicates() removes duplicate rows based on specified columns, while distinct() removes duplicate rows considering all columns.
Click to reveal answer
beginner
Why is it important to detect duplicate rows in data?
Duplicate rows can bias analysis and models by over-representing some data points. Removing duplicates ensures fair and accurate insights.
Click to reveal answer
intermediate
How can you count the number of nulls in each column of a Spark DataFrame?
Use aggregation with sum and when functions: df.select([F.sum(F.when(F.col(c).isNull(),1).otherwise(0)).alias(c) for c in df.columns]).show().
Click to reveal answer
Which Spark function helps to find rows with null values in a column?
AisNull()
BisEmpty()
CisNaN()
DisDuplicate()
✗ Incorrect
The isNull() function checks if a column value is null.
What does dropDuplicates(['col1', 'col2']) do in Spark?
ARemoves duplicate rows based on col1 and col2 values
BRemoves all duplicates in the DataFrame
CRemoves rows with nulls in col1 or col2
DRemoves rows where col1 equals col2
✗ Incorrect
It removes rows that have the same values in both col1 and col2.
Which method removes duplicate rows considering all columns in a Spark DataFrame?
AdropDuplicates()
Bdistinct()
CdropNulls()
DfilterDuplicates()
✗ Incorrect
distinct() removes duplicate rows based on all columns.
Why should you handle null values before analysis?
ANulls are always replaced with zeros automatically
BNulls improve model accuracy
CNulls increase dataset size
DNulls can cause errors or bias in results
✗ Incorrect
Null values can cause errors or bias if not handled properly.
How can you count nulls in each column of a Spark DataFrame?
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.