CertSafari
    Databricks Certified Associate Developer for Apache Spark· Lessons

    Domain 7 · Lesson 31/32

    Pandas API on Spark: advantages and limits

    Explain the advantages of using Pandas API on Spark.

    14 min read
    2.5% of exam
    6 sources
    Published 21 Oct 2026
    Docs as of 30 Sep 2026

    What you will be able to do

    • Explain which limitation of single-node pandas pyspark.pandas removes, and why that helps both pandas users and PySpark users
    • Move data between Spark DataFrames, pandas-on-Spark DataFrames and plain pandas, and say which conversions collect data to the driver
    • Explain why enabling Arrow speeds up pandas conversions
    • Recognise where Pandas API on Spark stops paying off: default index creation, row order, unsupported pandas behaviour and small data

    Key concept

    Pandas API on Spark (pyspark.pandas) — A pandas-compatible API whose DataFrames and Series are backed by Spark. You write the same pandas calls, but Spark runs them lazily as distributed jobs across the cluster, so a dataset no longer has to fit in one machine's memory.

    1.The limit of plain pandas that pyspark.pandas removes

    Plain pandas is a single-machine library. A pandas DataFrame lives entirely in the memory of the Python process that created it, and every method call runs immediately on that one machine. On a laptop with 16 GB of RAM, a 500 GB Delta table can't be loaded as a pandas DataFrame, and adding nodes to a cluster doesn't change that. pandas uses only the process it runs in.

    Pandas API on Spark keeps the pandas programming model and changes where the work runs. You import it with import pyspark.pandas as ps, and a pandas-on-Spark DataFrame looks and behaves like a pandas DataFrame. Underneath it is a Spark plan. Calls such as dropna(), groupby() and sum() become Spark transformations that executors run in parallel over partitions, so the data never has to fit in a single process. Pandas API on Spark has shipped with Apache Spark since 3.2. Before that it was a separate project called Koalas, so treat "Koalas" in older material as the same idea under its old name.

    The second difference is when the work runs. pandas evaluates eagerly: each line produces a finished in-memory result. Spark evaluates lazily: transformations build up a plan, and an action triggers the distributed computation. That lazy plan lets the engine optimise the whole chain before running it. It is also why some pandas habits that are cheap on one machine become expensive on a cluster.

    Checkpoint 1 of 8· Check yourself

    A data scientist's pandas script runs fine on a sample. On a 16-node Databricks cluster it fails with out-of-memory errors when loading a 500 GB Delta table. What is the underlying problem, and what does Pandas API on Spark change?

    Checkpoint 2 of 8· Exam question

    What is the primary limitation of native pandas that the Pandas API on Spark (pyspark.pandas) is designed to address?

    Sources12

    2.One codebase for pandas users and PySpark users

    For a pandas user, the main advantage is that existing code keeps working. Say a cleaning script calls df.dropna() and then df.groupby('region')['sales'].sum(). To run it against a large Delta table, read the table into a Spark DataFrame, call sdf.pandas_api(), and run the same calls on the result. You don't translate it into na.drop() and agg(), wrap it in a UDF, or collect the table to the driver first. The same code can run against plain pandas on a small sample in unit tests and against pandas-on-Spark in production.

    PySpark users gain something too. Some tasks that take extra work with the native DataFrame API are one call in pandas style. Plotting is one example: psdf.plot() works directly on distributed data. Other examples are descriptive summaries with describe(), transposing with .T, and label-based selection with an index. A PySpark engineer can switch to pandas syntax for one step and then switch back.

    Conversion between the two worlds is a single method call in each direction. DataFrame.pandas_api() turns a Spark DataFrame into a pandas-on-Spark DataFrame. Its optional index_col argument picks which Spark column becomes the index. psdf.to_spark() goes the other way and returns a regular Spark DataFrame that you can pass to Spark SQL, Structured Streaming sinks or MLlib.

    pandas_api() without and with index_col: the first call has pandas-on-Spark create a 0, 1, 2 index, the second promotes the age column to the indexpython
    df = spark.createDataFrame(
        [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
    
    df.pandas_api()
    #    age   name
    # 0   14    Tom
    # 1   23  Alice
    # 2   16    Bob
    
    df.pandas_api(index_col="age")
    #       name
    # age
    # 14     Tom
    # 23   Alice
    # 16     Bob
    Moving data between plain pandas, pandas-on-Spark and Spark DataFrames
    From → toCallWhere the data ends up
    Spark DataFrame → pandas-on-Sparksdf.pandas_api()Stays distributed on the cluster
    pandas → pandas-on-Sparkps.from_pandas(pdf)Distributed from the driver's local pandas object
    pandas → Spark DataFramespark.createDataFrame(pdf)Distributed from the driver's local pandas object
    Spark DataFrame → pandastoPandas()Collected into driver memory as plain pandas

    Checkpoint 3 of 8· Check yourself

    Your team has a pandas cleaning script that calls df.dropna() and then df.groupby('region')['sales'].sum(). The unit tests run it on small pandas samples. Now it has to run in production against a multi-terabyte Delta table. Which approach reuses the script and keeps the data distributed?

    Checkpoint 4 of 8· Fill the gap

    The second conversion makes the age column the index of the pandas-on-Spark DataFrame instead of a default 0, 1, 2 index. Which argument fills the blank?

    df = spark.createDataFrame(
        [(14, "Tom"), (23, "Alice"), (16, "Bob")], ["age", "name"])
    
    df.pandas_api()
    #    age   name
    # 0   14    Tom
    # 1   23  Alice
    # 2   16    Bob
    
    df.pandas_api( ? ="age")
    #       name
    # age
    # 14     Tom
    # 23   Alice
    # 16     Bob

    Checkpoint 5 of 8· Exam question

    A data scientist has an existing pandas script that cleans a DataFrame using df.dropna() followed by df.groupby('region')['sales'].sum(). They want to reuse this exact logic against a 500 GB Delta table without rewriting the pandas calls. Which change lets them do this with the pandas API on Spark?

    Sources213

    3.Arrow and the cost of crossing into Python

    Spark runs in the JVM and pandas runs in Python, so data that crosses between them has to be serialised. Without Arrow, rows are converted one at a time. Apache Arrow is a columnar in-memory format that both sides can read, so whole column batches move at once. Pandas API on Spark uses the same PySpark configuration as the rest of the session, so the Arrow switch spark.sql.execution.arrow.pyspark.enabled speeds up its internal pandas conversions too.

    The same 300,000-row conversion timed with Arrow on (about 900 ms) and off (about 3.08 s)python
    spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", True)
    %timeit ps.range(300000).to_pandas()
    
    900 ms ± 186 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
    [36]:
    
    spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", False)
    %timeit ps.range(300000).to_pandas()
    
    3.08 s ± 227 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

    Arrow speeds up the transfer, but it doesn't change where the data goes. A conversion to plain pandas still lands every row on the driver. Results are the same with Arrow on or off. Some data types aren't supported by Arrow, and when Spark hits one it can fall back to the slower path without raising an error. The fallback is controlled by spark.sql.execution.arrow.pyspark.fallback.enabled. Shuffle partitions, driver heap, AQE and storage levels don't affect the JVM-to-Python serialisation cost.

    Checkpoint 6 of 8· Check yourself

    With spark.sql.execution.arrow.pyspark.enabled set to true, an engineer plans to call to_pandas() on a 2 TB pandas-on-Spark DataFrame, reasoning that the conversion is now fast enough. What does Arrow actually change about this call?

    Checkpoint 7 of 8· Exam question

    An engineer benchmarks converting a 300,000-row Spark DataFrame to a pandas-on-Spark DataFrame and finds the operation takes several seconds. A teammate suggests a configuration change that previously cut a similar conversion time from about 3 seconds to under 1 second. Which setting most likely explains that speedup?

    Sources45

    4.Where the advantages stop: index, order and small data

    Most of the hidden cost comes from the index. Every pandas DataFrame has one, and a Spark DataFrame has none. If you call pandas_api() without index_col, or read a file through ps, pandas-on-Spark has to create a default index, and creating it is real distributed work. The option compute.default_index_type controls which kind you get. A sequence index numbers rows 0, 1, 2… by moving the data through a single partition, which stops scaling on large data. distributed-sequence also produces consecutive numbers but computes them across the cluster, which still costs an extra pass. distributed is the cheapest: it assigns increasing IDs that aren't consecutive and that can differ between runs. To skip index creation entirely, pass a real key column as index_col.

    Row order is the second limit. A pandas DataFrame keeps rows in the order they were loaded. A Spark DataFrame spread over partitions has no natural order, so head() may not return the first rows of the file. You can turn on compute.ordered_head to make it do so, but that adds an internal sort to every call. Operations whose meaning depends on position, such as positional slicing or comparing a row with its neighbour, pay for that ordering on large data. Some pandas features aren't implemented at all or behave differently. For example, an operation that combines two different DataFrames is blocked by default, because it requires a join. For these gaps, native PySpark gives you more control.

    Size is the last limit. For a few hundred megabytes that fit comfortably in memory, plain pandas on a single node is faster: it skips job scheduling, task launches and the JVM-to-Python transfer. Pandas API on Spark pays off once the data, or the memory pandas needs to process it, is too large for one machine. For data that small, Spark itself is often the wrong tool.

    Checkpoint 8 of 8· Check yourself

    After an analyst moves a pandas report to pandas-on-Spark, psdf.head() no longer reliably shows the first five rows of the source file. What explains this, and what does restoring the pandas behaviour cost?

    Sources365

    Sources

    (6)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “pandas does not scale out to big data. Pandas API on Spark fills this gap”
      ↩︎ The limit of plain pandas that pyspark.pandas removes
      “Pandas API on Spark is useful not only for pandas users but also PySpark users”
      ↩︎ One codebase for pandas users and PySpark users
      “pandas does not scale out to big data. Pandas API on Spark fills this gap”
      ↩︎ Key concept
    2. 2.
      “This varies drastically from eager execution, which is the model used by pandas DataFrames.”
      ↩︎ The limit of plain pandas that pyspark.pandas removes
      “with a single codebase that works with pandas (tests, smaller datasets) and with Spark (production, distributed datasets)”
      ↩︎ One codebase for pandas users and PySpark users
    3. 3.
      “Converts the existing DataFrame into a pandas-on-Spark DataFrame.”
      ↩︎ One codebase for pandas users and PySpark users
      “it will lose the index information and the original index will be turned into a normal column.”
      ↩︎ Where the advantages stop: index, order and small data
      “Converts the existing DataFrame into a pandas-on-Spark DataFrame.”
      ↩︎ Exam trap 1
    4. 4.
      “Apache Arrow is an in-memory columnar data format used in Apache Spark to efficiently transfer data between JVM and Python processes.”
      ↩︎ Arrow and the cost of crossing into Python
      “toPandas() results in the collection of all records in the DataFrame to the driver program”
      ↩︎ Exam trap 2
      “Apache Arrow is an in-memory columnar data format used in Apache Spark to efficiently transfer data between JVM and Python processes.”
      ↩︎ Exam trap 3
      “Even with Arrow, toPandas() results in the collection of all records in the DataFrame to the driver program”
      ↩︎ Checkpoint
    5. 5.
      “you can enable Arrow optimization to hugely speed up internal pandas conversion.”
      ↩︎ Arrow and the cost of crossing into Python
      “The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.”
      ↩︎ Where the advantages stop: index, order and small data
      “The natural order can be preserved by setting compute.ordered_head option but it causes a performance overhead with sorting internally.”
      ↩︎ Exam trap 4
    6. 6.
      “PySpark is more flexible than the Pandas API on Spark”
      ↩︎ Where the advantages stop: index, order and small data
      “For small workloads which only require single nodes, data scientists can use single node compute for cost savings.”
      ↩︎ Where the advantages stop: index, order and small data

    Ready to test yourself?

    Practise the 11 questions on this subdomain.

    Spotted a mistake, or was something unclear? Tell us.