【发布时间】:2020-05-26 19:34:06
【问题描述】:
我有一个数据框,在其中我通过多个 .withColumn 链重复将相同的过程应用于多个列,就像这样
df= dt.withColumn('PL_start', from_unixtime('PL_start', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('PL_end??' , from_unixtime('PL_end', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('MU_start', from_unixtime('MU_start', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('MU_end' , from_unixtime('MU_end', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('PU_start', from_unixtime('PU_start', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('PU_end' , from_unixtime('PU_end', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('RE_start', from_unixtime('RE_start', "yyyy-MM-dd HH:mm:ss")) \
.withColumn('RE_end' , from_unixtime('RE_end', "yyyy-MM-dd HH:mm:ss")) \
.withColumn(...)
我发现了这个repeated calls to withColumn() using the same function on multiple columns 这个问题有点老了。想知道在新版本的 Spark 2.4.3 中是否有解决此问题的方法?
【问题讨论】:
-
试试
cols=['MU_end','PU_start','PU_end','RE_start']dt.select(*[from_unixtime(x).alias(x) for x in cols]) -
如果您希望所有其他列都包含所有新更改的列,
cols=['MU_end','PU_start','PU_end','RE_start']dt.select(*[x for x in dt.columns if x not in cols],*[from_unixtime(x).alias(x) for x in cols]))
标签: python-3.x apache-spark pyspark