Menu
CoddyTech

Missing Values

Part of the Fundamentals section of Coddy's PySpark journey. Lesson 8 of 18.

A missing value is represented by null in Spark and None in collected Python rows. Null is not zero and not an empty string. Comparisons with null do not evaluate to true, so a filter such as stock > 0 excludes missing stock.

known = items.filter(F.col("stock").isNotNull())
filled = items.fillna({"stock": 0})

isNull() and isNotNull() test missingness. fillna replaces null values in the specified columns. A supplied schema lets Spark build even an empty table or a column whose values are all missing.

challenge icon

Challenge

Easy

Replace missing stock with 0. Preserve every row and both columns. Do not change non-null values.

Write transform(df) and return a Spark DataFrame. The checker supplies df with schema item string, stock int. The session and F (PySpark functions) are already available. Do not print; the checker displays the returned rows. Return columns in this order: item, stock.

Try it yourself

def transform(df):
    return df
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online Python compiler