如何将星期几和数字放在同一列Pyspark中



我有以下代码:

identified_new = (spark.table(f'nn_team_{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)
.join(dim_customers,'customer_id','inner')
.withColumn('week', f.date_format(f.date_sub(f.col('date_key'), 4), 'Y-ww'))
.withColumn('day', f.date_format(f.date_sub(f.col('date_key'), 4), 'DD-ww'))
)
output_new_users = (identified_new
.groupby('week','day')
.agg(
f.countDistinct('customer_id').alias('new_users'),
f.countDistinct('ticket_id').alias('total_tickets'),
f.count('ticket_id').alias('tickets')
)
)
display(output_new_users)

实际输出:

week    day         new_users   total_tickets   tickets
2020-51 350-51        31662      34748           34748
2020-51 348-51        50451      55995           55995
2020-51 349-51        49476      55106           55106
2020-51 351-51        23297      25282           25282
2020-50 347-50        40006      43713           43713
2020-50 346-50        41971      46044           46044
2020-50 345-50        51463      57234           57234

我想得到的是同一单元格中的星期几和月份号。查看所需输出:

week    day                new_users    total_tickets   tickets
2020-51 Monday    14th        31662      34748           34748
2020-51 Tuesday   15th        50451      55995           55995
2020-51 Wednesday 16th        49476      55106           55106
2020-51 Thursday  17th        23297      25282           25282
2020-50 Friday    18th        40006      43713           43713
2020-50 Saturday  19th        41971      46044           46044
2020-50 Sunday    20th        51463      57234           57234

有办法在pyspark上做到这一点吗?谢谢

更改此行

.withColumn('day', f.date_format(f.date_sub(f.col('date_key'), 4), 'DD-ww'))

.withColumn('day', f.date_format(f.date_sub(f.col('date_key'), 4), 'EEEE dd'))

使得CCD_ 1列具有期望的格式。

有关日期格式字符串的更多详细信息,请参阅文档。

最新更新