如何在PySpark中创建一个新列并使用今天的日期填充此列?
这是我试过的:
- import datetime
- now = datetime.datetime.now()
- df = df.withColumn("date",str(now)[:10])
我收到此错误:
AssertionError: col should be Column
解决方法
How to create a new column in PySpark and fill this column with the date of today?
已有功能:
- from pyspark.sql.functions import current_date
- df.withColumn("date",current_date().cast("string"))
AssertionError: col should be Column
使用文字
- from pyspark.sql.functions import lit
- df.withColumn("date",lit(str(now)[:10]))