PySpark 入門
Benjamin Schmidt
Data Engineer
UDF (ユーザー定義関数): PySparkデータフレームを使用してデータを扱うためのカスタム関数
UDFの利点:
すべてのPySpark UDFは、udf()関数を使用して登録する必要があります。
# Define the function def to_uppercase(s): return s.upper() if s else None# Register the function to_uppercase_udf = udf(to_uppercase, StringType())# Apply the UDF to the DataFrame df = df.withColumn("name_upper", to_uppercase_udf(df["name"]))# See the results df.show()
覚えておく: UDFを使用すると、PySpark DataFrame{{5}} にカスタムPythonロジックを適用できます
from pyspark.sql.functions import pandas_udf
@pandas_udf("float")
def fahrenheit_to_celsius_pandas(temp_f):
return (temp_f - 32) * 5.0/9.0
udf()PySpark 入門