映射 DataFrame 中单个列的正确方法是什么?

What's the proper way to map over a single column in a DataFrame?

通常我会做类似的事情

val fun = udf { x => ... }
df.withColumn("new", fun(df.col("old"))).drop("old").withColumnRename("new", "old")

有没有更短的路?

我通常会做以下事情:

val df : DataFrame = ???
val fun = udf { x => ... }
df.withColumn("old", fun(df.col("old")))

但是您会丢失旧专栏中的信息,因此请注意不要丢失宝贵的日期。

PS: 当然,在 Spark 中可以通过不同的方式访问列。所以我让你决定使用哪个。