from pyspark.sql.functions import udf
from pyspark.sql.types import *
schema = StructType([StructField('foo', FloatType(), False), StructField(
    'bar', FloatType(), False)])

def udf_test(n):
    return (n / 2, n % 2) if n and n != 0.0 else (float('nan'), float('nan'))
test_udf = udf(udf_test, schema)
df = sc.parallelize([(1, 2.0), (2, 3.0)]).toDF(['x', 'y'])
foobars = df.select(test_udf('y').alias('foobar'))
foobars.printSchema()
Apache Spark -- Assign the result of UDF to multiple dataframe columns