Skip to main content
InterviewHubPRO
Section Overview
Q30

Question 30 of 50

Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

60%
Apache Spark Big Data (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

How do you implement custom User-Defined Functions (UDFs) vs Pandas Vectorized UDFs in PySpark?

senior LevelMost AskedCode ExampleAlgorithms

In Simple Words:

  • Standard Python UDF: Extremely slow! Deserializes JVM objects to Python processes row-by-row through IPC pipes.
  • Pandas Vectorized UDF (PyArrow): Transmits chunks of columnar data in Apache Arrow format directly into vectorized Pandas / NumPy arrays, running up to 100x faster.
python
from pyspark.sql.functions import pandas_udf
import pandas as pd

# Vectorized Pandas UDF:
@pandas_udf("double")
def calculate_vat(price: pd.Series) -> pd.Series:
    return price * 0.20

df = df.withColumn("vat", calculate_vat(df["price"]))