Pyspark:列不可迭代(expr,format_string)

wbgh16ku  于 5个月前  发布在  Spark
关注(0)|答案(1)|浏览(62)

我正在做一个Spark的框架,看起来像这样:
x1c 0d1x的数据
我想subscurt(如果它的utc-)/add(如果它的utc+)“utc_time”小时量到“local 2”日期时间。我想使用expr函数来完成。我想使用的是,如果“utc”是+,将“utc_time”添加到“local 2”日期时间。下面你可以看到我的代码:

df = df.withColumn('updated_local', when((substring('utc', 4, 1))=='+',(df.local2 + expr(format_string("INTERVAL %s HOURS", "utc_time"))) ).otherwise('whatever'))

字符串
但我得到一个'列是不可迭代'的错误时,使用这个。
如果我不使用格式字符串并像这样构造它,它就可以工作:

df = df.withColumn('updated_local', when((substring('utc', 4, 1))=='+',(df.local2 + expr("INTERVAL 10 HOURS")) ).otherwise('whatever'))


我喜欢expr函数以外的方法,如果你们有的话。

wkftcu5l

wkftcu5l1#

你可以使用下面的代码,它应该工作:

from pyspark.sql.functions import *
 df.withColumn('updated_local', when((substring(col('utc'), 4, 1))=='+', concat(col('local2'), lit("INTERVAL "), col('utc_time'), lit(" HOURS utc_time"))).otherwise('whatever'))

字符串
问候

相关问题