Skip to content

Instantly share code, notes, and snippets.

@afranzi
Last active January 31, 2019 14:47
Show Gist options
  • Save afranzi/040e0a6f0e7a7268cf089e38f55ca787 to your computer and use it in GitHub Desktop.
Save afranzi/040e0a6f0e7a7268cf089e38f55ca787 to your computer and use it in GitHub Desktop.
from pyspark.sql.types import StringType
from pyspark.sql.functions import udf
# 1.- UDF with f as a lambda
to_upper = udf(lambda s: s.upper() if s else None, StringType())
# 2.- UDF with f as a method
def to_upper(s):
if s is not None:
return s.upper()
to_upper = udf(to_upper)
# 3.- function using the @udf annotation
@udf(returnType=StringType())
def to_upper(s):
if s is not None:
return s.upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment