How do we decorate a Python phone sanitization function with @udf and explicit StringType return type?
The @udf decorator converts a Python function into a Spark Column User-Defined Function.
Applying complex custom Python parsing rules directly across DataFrame columns.
from pyspark.sql.functions import udf
from pyspark.sql.types import StringType
@udf(returnType=StringType())
def sanitize_phone(phone):
return "".join(c for c in phone if c.isdigit()) if phone else NonePractice typing production-grade PySpark code for Decorating Functions with @udf.