Dataframe 如何选择周的日期
我需要为一周中的某一天创建一列,其中的值为周一、周二、周三 然后只在星期五使用过滤器 我使用的代码如下所示:Dataframe 如何选择周的日期,dataframe,apache-spark,pyspark,apache-spark-sql,Dataframe,Apache Spark,Pyspark,Apache Spark Sql,我需要为一周中的某一天创建一列,其中的值为周一、周二、周三 然后只在星期五使用过滤器 我使用的代码如下所示: 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')
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('HP_client', f.col('customer_id').isNotNull())
.withColumn('local_time',f.from_utc_timestamp(f.col('trx_begin_date_time'),'Europe/Brussels'))
.withColumn('Hour', f.hour(f.col('local_time')))
.withColumn('Day', f.day(f.col('local_time')))
.filter(f.col('Hour').between(4, 8))
)
以下是我得到的错误:
AttributeError:模块“pyspark.sql.functions”没有属性“day”
我如何为dayli创建列?谢谢您可以使用
F.dayofweek
,它返回一个整数(1=星期日,2=星期一,…,7=星期六)
或者,您可以使用F.date\u格式('local\u time','E')
,该格式返回类似'Sun','Mon',
等字符串
'EEEE'
返回完整的字符串,例如Sunday
等