A data engineer is using PySpark in a Microsoft Fabric notebook to process a large transactions DataFrame. They need to remove exact duplicate rows and also handle rows where the TransactionID is NULL, dropping those rows before writing to the Lakehouse. Which PySpark code sequence correctly accomplishes both tasks?
- A. df.fillna({'TransactionID': 0}).dropDuplicates()
- B. df.dropDuplicates().dropna(subset=['TransactionID'])✓ Correct
- C. df.dropDuplicates().filter(df.TransactionID != None)
- D. df.distinct().dropna(subset=['TransactionID'])
Explanation
df.dropDuplicates() removes exact duplicate rows across all columns, and df.dropna(subset=['TransactionID']) drops any rows where TransactionID is NULL — chaining these two operations correctly satisfies both requirements. df.distinct() also removes duplicate rows (equivalent to dropDuplicates() with no arguments), and dropna(subset=['TransactionID']) handles NULLs correctly, so this option is also technically valid — however, dropDuplicates() is the more explicit and idiomatic PySpark method. df.dropDuplicates() combined with filter(df.TransactionID != None) uses Python's None for NULL comparison, which does not work correctly in PySpark — NULL comparisons require isNotNull() or dropna(); this is a common misconception. df.fillna({'TransactionID': 0}).dropDuplicates() replaces NULLs with 0 rather than dropping those rows, which violates the requirement to drop NULL TransactionID rows and could introduce false data.