【问题标题】:Include filter after withcolumn Pyspark在 withcolumn Pyspark 之后包含过滤器
【发布时间】:2021-03-12 22:57:02
【问题描述】:

我正在尝试创建一列,然后应用过滤器,但出现错误:

TypeError: when() 缺少 1 个必需的位置参数:'value'

这里是使用的代码:

df = (
      spark.table(f'nn_squad7_{country}.fact_table')
     .filter(f.col('date_key').between(start,end))
     .filter(f.col('is_client_plus')==1)
     .filter(f.col('source')=='tickets')
     .filter(f.col('subtype')=='trx')
     .filter(f.col('is_trx_ok') == 1) 
     .withColumn('week', f.date_format(f.date_sub(f.col('date_key'), 1), 'YYYY-ww'))
     .withColumn('month', f.date_format(f.date_sub(f.col('date_key'), 1), 'M'))
     .withColumn('local_time',f.from_utc_timestamp(f.col('trx_begin_date_time'),'Europe/Brussels'))
     .withColumn('Hour', f.hour(f.col('local_time')))
     .filter(f.when(f.col('Hour')>= 4) & (f.col('Hour')<= 8))
     )

我尝试应用的过滤器与客户的营业时间有关,我只想知道每个星期五在4.008.00 pm 之间发生的数据。因此,如果此过滤器有效,我可能必须在一周中的某一天添加​​另一个过滤器。

关于错误的任何线索?

【问题讨论】:

    标签: python dataframe apache-spark pyspark apache-spark-sql


    【解决方案1】:

    您收到此错误是因为您只将一个参数(条件)传递给 when 函数,而它需要 2 个。但是,此处无需使用 when,只需使用 between

    .filter(f.col('Hour').between(4, 8))
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2021-12-24
      • 2017-03-18
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-30
      • 1970-01-01
      相关资源
      最近更新 更多