Skip to content

Instantly share code, notes, and snippets.

@mickdelaney
Forked from zoltanctoth/pyspark-udf.py
Created January 8, 2017 20:13
Show Gist options
  • Select an option

  • Save mickdelaney/4c29b906a4432880f04bf65fa92766e2 to your computer and use it in GitHub Desktop.

Select an option

Save mickdelaney/4c29b906a4432880f04bf65fa92766e2 to your computer and use it in GitHub Desktop.
Writing an UDF for withColumn in PySpark
from pyspark.sql.types import StringType
from pyspark.sql.functions import udf
maturity_udf = udf(lambda age: "adult" if age >=18 else "child", StringType())
df = sqlContext.createDataFrame([{'name': 'Alice', 'age': 1}])
df.withColumn("maturity", maturity_udf(df.age))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment