Concept Flow - Data quality assertions
Load DataFrame
Define Assertions
Apply Assertions on Data
Check Assertion Results
Continue
Load data, define checks (assertions), apply them, then handle pass or fail results.
from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() df = spark.createDataFrame([(1, 'A'), (2, None)], ['id', 'value']) assert df.filter(df.value.isNull()).count() == 0, 'Null values found!'
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Load DataFrame with 2 rows | DataFrame created | Rows: 2, Columns: id, value |
| 2 | Define assertion: no nulls in 'value' | Prepare filter condition | Condition ready |
| 3 | Filter rows where 'value' is null | Filter result count = 1 | One row has null |
| 4 | Check if count == 0 | 1 == 0 | False |
| 5 | Assertion fails | Raise error 'Null values found!' | Error raised, stop execution |
| Variable | Start | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|
| df | Empty | DataFrame with 2 rows | Same | Same |
| null_count | Undefined | 1 | 1 | 1 |
| assertion_result | Undefined | Undefined | False | Error raised |
Data quality assertions check if data meets rules. Use filters to find bad data (e.g., nulls). Assert conditions must be True to pass. If False, raise error to stop or handle. Helps catch data issues early.
age has no null values?isNull(). Counting rows where age is null should be zero for no nulls.age is zero. df.filter(df.age.isNotNull()).count() == 0 checks for non-null count equals zero, which is wrong. Options C and D use null with == or != which is invalid in Spark.from pyspark.sql import SparkSession spark = SparkSession.builder.getOrCreate() data = [(1, 'Alice', 25), (2, 'Bob', None), (3, 'Charlie', 30)] df = spark.createDataFrame(data, ['id', 'name', 'age']) null_count = df.filter(df.age.isNull()).count() print(null_count)
age is None (null): the second row with Bob.df.age.isNull() selects rows with null age, so count will be 1.salary are positive:
assert df.filter(df.salary <= 0).count() == 0But it passes even when there are null values in the salary column. What is the likely cause?
null <= 0 evaluates to null, which is falsy in filter(), so null rows are skipped and not counted, causing the assertion to pass incorrectly when nulls exist.count() is supported on DataFrames. D: assert syntax is correct. B identifies the null-skipping issue.salary contains null values, which are skipped by the comparison -> Option Adf has no duplicate user_id values. Which Spark code correctly performs this data quality assertion?user_id appear more than once. Grouping by user_id and counting, then filtering counts > 1 finds duplicates.df.dropDuplicates().count() == df.count() checks for completely identical rows across all columns, missing duplicates in user_id with differing other columns. B: df.select('user_id').count() == df.count() always true. C: Checks only nulls. D: Correctly finds groups with count > 1.Pass the test and this pattern moves unknown