QuestionQ50

VerifiedDeveloping Apache Spark™ DataFrame/DataSet API Applications

A developer wants to refactor older Spark code to use built-in functions introduced in Spark 3.5.0.

The developer finds the following existing DataFrame code:

import pyspark.sql.functions as F  
  
min_price = 110.50  
  
result_df = prices_df \  
.filter(F.col("spot_price") >= F.1it(min_price)) \  
.agg(F.count ("*"))  

Which code block should the developer use to refactor the code?

Explanation

count_if is a Spark 3.5.0 aggregate function that returns the number of rows for which its Boolean expression is TRUE. Counting rows where spot_price >= min_price is equivalent to filtering on that predicate and applying count("*").

Learn more

Community Discussion

No comments yet. Be the first to start the discussion!