python - 如何在同一列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'))
使该day
列具有所需的格式。
有关日期格式字符串的更多详细信息可以在docs中找到。
推荐阅读
- python - 为什么计划功能挂起,并且从不打印 csv?
- php - 数据库中的价格不适用于智能贝宝按钮
- c# - 如何获取 Cylance 用户 API 的访问令牌?
- java - 如何在 Java Youtube Search API 的查询词中使用短语?
- api - THINGSBOARD REST API CALL 节点
- powershell - How to put computers on a network into a variable in PowerShell?
- excel - 如何仅为一张工作表启用 Excel 宏
- excel - 我可以让我的 VBA 代码更快地工作吗?目前浏览 1300 行和 500 列需要 7 分钟
- php - 重新发送电子邮件的功能将我带到主页并且在给定的功能中什么都不做 - Laravel
- mule - Mule4 HTTP 连接器和 Curl/Postman/ARC 得到不同的结果