Filter Rows
Part of the Fundamentals section of Coddy's PySpark journey. Lesson 5 of 18.
Use filter with a column expression to keep matching rows. F.col("score") > 10 compares each row’s score with a number. It is not a single Python boolean. F is the conventional alias for pyspark.sql.functions.
from pyspark.sql import functions as F
passed = scores.filter(F.col("score") >= 60)Combine column conditions with & for both and | for either. Put each comparison in parentheses. Python and and or cannot combine Spark column expressions.
Challenge
EasyKeep rows whose stock is greater than 0. Preserve all columns. Zero stock does not qualify.
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
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
2Transforming Data
Select ColumnsFilter RowsDerived ColumnsSort RowsMissing ValuesRecap: Stock ReportPractice on your own: Online Python compiler