这是我的数据集:
from pyspark.sql import SparkSession, functions as F
spark = SparkSession.builder.getOrCreate()
df = spark.createDataFrame([('2021-02-07',),('2021-02-08',)], ['date']) \
.select(
F.col('date').cast('date'),
F.date_format('date', 'EEEE').alias('weekday'),
F.dayofweek('date').alias('weekday_number')
)
df.show()
#+----------+-------+--------------+
#| date|weekday|weekday_number|
#+----------+-------+--------------+
#|2021-02-07| Sunday| 1|
#|2021-02-08| Monday| 2|
#+----------+-------+--------------+
dayofweek
返回从星期日开始的工作日数字。
如何从星期一而不是星期日开始返回工作日数字?IE
+----------+-------+--------------+
| date|weekday|weekday_number|
+----------+-------+--------------+
|2021-02-07| Sunday| 7|
|2021-02-08| Monday| 1|
+----------+-------+--------------+