Actually this approach works just fine in Python:
from pyspark.sql.functions import array, udf
df = sc.parallelize([("a", "b", "c", "d")]).toDF()
f = udf(lambda xs: "+".join(xs))
df.select(f("_1")).show()
## +------------+
## |<lambda>(_1)|
## +------------+
## | a|
## +------------+
df.select(f(array("_1", "_2"))).show()
## +-----------------------+
## |<lambda>(array(_1, _2))|
## +-----------------------+
## | a+b|
## +-----------------------+
df.select(f(array("_1", "_2", "_3"))).show()
## +---------------------------+
## |<lambda>(array(_1, _2, _3))|
## +---------------------------+
## | a+b+c|
## +---------------------------+
Since Python UDF are not the same type of entity like their Scala counterpart are not constrained by the types and number of the input arguments you also use args:
g = udf(lambda *xs: "+".join(xs))
df.select(g("_1", "_2", "_3", "_4")).show()
## +------------------------+
## |<lambda>(_1, _2, _3, _4)|
## +------------------------+
## | a+b+c+d|
## +------------------------+
to avoid wrapping input with array
.
You can also use struct
as an alternative wrapper to get access to the column names:
h = udf(lambda row: "+".join(row.asDict().keys()))
df.select(h(struct("_1", "_2", "_3"))).show()
## +----------------------------+
## |<lambda>(struct(_1, _2, _3))|
## +----------------------------+
## | _1+_3+_2|
## +----------------------------+