databricks Associate Developer for Apache Spark · Practice questions

Section 7: Using Pandas API on Spark Practice Questions

Review 10 sample questions with their correct answers and explanations. Sign up free to continue with the full topic quiz.

Sign up free for the full quiz

Question 1

Which pandas UDF type processes an iterator of batches, useful when expensive initialization such as loading a model should happen once per partition?

  1. Series to scalar with a broadcast model
  2. Grouped map with applyInPandas only
  3. Row-at-a-time UDF
  4. Iterator of Series to Iterator of Series — Correct

Correct answer

Iterator of Series to Iterator of Series

Explanation

The iterator form lets the function run setup code once and then process multiple batches.

Question 2

Which method applies a Python function that takes and returns a pandas DataFrame to each group of a Spark DataFrame?

  1. df.groupBy('k').applyInPandas(func, schema) — Correct
  2. df.groupBy('k').mapInPandas(func, schema)
  3. df.applyPandas('k', func)
  4. df.groupBy('k').agg(func)

Correct answer

df.groupBy('k').applyInPandas(func, schema)

Explanation

applyInPandas runs a function on each group as a pandas DataFrame and returns a DataFrame matching the schema.

Question 3

Which import is used for the pandas API on Spark?

  1. import pandas.spark as ps
  2. from pyspark.sql import pandas as ps
  3. import pyspark.pandas as ps — Correct
  4. import koalas.spark as ps

Correct answer

import pyspark.pandas as ps

Explanation

The pandas API on Spark lives in pyspark.pandas (it evolved from the Koalas project).

Question 4

Which code reads a CSV file into a pandas-on-Spark DataFrame?

  1. ps.read_csv('/data/sales.csv') — Correct
  2. ps.load('/data/sales.csv', 'csv')
  3. pd.read_csv('/data/sales.csv').to_spark()
  4. spark.read.pandas('/data/sales.csv')

Correct answer

ps.read_csv('/data/sales.csv')

Explanation

pyspark.pandas provides read_csv, read_parquet and similar functions that load data in a distributed way.

Question 5

What is an advantage of the pandas API on Spark?

  1. It guarantees identical performance to pandas on small data
  2. It supports every pandas function with identical behavior
  3. pandas syntax on data too big for one machine — Correct
  4. It removes the need for a Spark cluster

Correct answer

pandas syntax on data too big for one machine

Explanation

pyspark.pandas provides a pandas-like API backed by distributed Spark execution. Some behaviors and functions differ from pandas.

Question 6

Which statement about the default index in pandas API on Spark is correct?

  1. Indexes are not supported
  2. Creating it can cost extra computation; its type is configurable — Correct
  3. Every pandas-on-Spark DataFrame is indexed by a Spark partition ID that is free to compute
  4. The index is always stored in driver memory

Correct answer

Creating it can cost extra computation; its type is configurable

Explanation

pandas-on-Spark attaches a default index (sequence, distributed-sequence or distributed), each with different cost trade-offs.

Question 7

Which statement about the pandas API on Spark compared with pandas is correct?

  1. It requires converting data to RDDs first
  2. It loads all data into the driver like pandas
  3. Execution is distributed and some pandas behaviors differ — Correct
  4. It supports only reading data, not transformations

Correct answer

Execution is distributed and some pandas behaviors differ

Explanation

pandas-on-Spark executes with Spark; ordering and some APIs differ from single-node pandas.

Question 8

Which Spark component lets pandas users run familiar pandas-style code on distributed data?

  1. Spark Core RDD API
  2. Structured Streaming
  3. GraphX
  4. Pandas API on Spark — Correct

Correct answer

Pandas API on Spark

Explanation

The pandas API on Spark (pyspark.pandas) implements much of the pandas API on top of Spark DataFrames.

Question 9

Which code converts a Spark DataFrame sdf into a pandas-on-Spark DataFrame without collecting it to the driver?

  1. sdf.pandas_api() — Correct
  2. sdf.toPandas()
  3. pd.DataFrame(sdf)
  4. sdf.collect().to_pandas()

Correct answer

sdf.pandas_api()

Explanation

pandas_api() returns a pyspark.pandas DataFrame that stays distributed. toPandas collects data to the driver.

Question 10

Which pandas UDF type is used for grouped aggregation that returns one value per group?

  1. Iterator of Series to Iterator of Series
  2. DataFrame to Row
  3. Series to scalar — Correct
  4. Series to Series

Correct answer

Series to scalar

Explanation

A Series-to-scalar pandas UDF can be used with groupBy().agg() to return one value per group.

Ready for the full quiz?

Create a free QuizAnalyst account to practice the complete question set and save your progress.

Sign up free for the full quiz