नोट
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप साइन इन करने या निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
इस पेज तक पहुँच के लिए प्रमाणन की आवश्यकता होती है. आप निर्देशिकाओं को बदलने का प्रयास कर सकते हैं.
Call a user-defined function.
Syntax
import pyspark.sql.functions as sf
sf.call_udf(udfName=<udfName>, *cols)
Parameters
| Parameter | Type | Description |
|---|---|---|
udfName |
str |
Name of the user defined function (UDF). |
cols |
pyspark.sql.Column or str |
Column names or Columns to be used in the UDF. |
Returns
pyspark.sql.Column: result of executed udf.
Examples
Example 1: Using call_udf with an integer UDF.
from pyspark.sql.functions import call_udf, col
from pyspark.sql.types import IntegerType, StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("intX2", lambda i: i * 2, IntegerType())
df.select(call_udf("intX2", "id")).show()
+---------+
|intX2(id)|
+---------+
| 2|
| 4|
| 6|
+---------+
Example 2: Using call_udf with a string UDF.
from pyspark.sql.functions import call_udf, col
from pyspark.sql.types import IntegerType, StringType
df = spark.createDataFrame([(1, "a"),(2, "b"), (3, "c")],["id", "name"])
_ = spark.udf.register("strX2", lambda s: s * 2, StringType())
df.select(call_udf("strX2", col("name"))).show()
+-----------+
|strX2(name)|
+-----------+
| aa|
| bb|
| cc|
+-----------+