【问题标题】:PySpark UDF to multiple columnsPySpark UDF 到多列
【发布时间】:2018-01-04 06:17:36
【问题描述】:

所以我有一个这样的 UDF:

 tudf = udf(lambda value: 1 if value>=1 else 0,IntegerType())

我通常只是像这样传递 UDF:

 df = fdf.withColumn('COLUMN1',tudf(df.COLUMN1))

我想知道是否有任何方法可以做到这一点,但不必一一列出。

【问题讨论】:

    标签: apache-spark pyspark


    【解决方案1】:

    使用理解:

    fdf.select([
      tudf(c).alias(c) if c in cols_to_transform else c for c in fdf.columns
    ])
    

    虽然这里不推荐udf

    from pyspark.sql.functions import when, col
    
    fdf.select([
      when(col(c) >= 1, 1).otherwise(0).alias(c) if c in cols_to_transform else c 
      for c in fdf.columns
    ])
    

    【讨论】:

    • 谢谢你,伙计。不推荐是因为计算量大吗?
    猜你喜欢
    • 2017-07-21
    • 2020-03-31
    • 2018-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2021-03-08
    相关资源
    最近更新 更多