Rah*_*dar 4 dataframe apache-spark apache-spark-sql pyspark
我正在尝试使用 withColumn 方法在现有的 Pyspark Dataframe 中添加一列。我想在此列中插入当前日期。从我的来源来看,我没有任何日期列,因此我在数据框中添加此当前日期列并保存这个数据框在我的表中,以便稍后出于跟踪目的我可以使用当前日期列。我正在使用下面的代码
df2=df.withColumn("Curr_date",datetime.now().strftime('%Y-%m-%d'))
Run Code Online (Sandbox Code Playgroud)
这里 df 是我现有的数据框,我想将 df2 保存为带有 Curr_date 列的表。但这里它期望现有的列或 lit 方法而不是 datetime.now().strftime('%Y-%m-%d')。有人请指导我如何在我的数据框中添加此日期列。?
from pyspark.sql import functions as F
df2 = df.withColumn("Curr_date", F.lit(datetime.now().strftime("%Y-%m-%d")))
# OR
df2 = df.withColumn("Curr_date", F.current_date())
Run Code Online (Sandbox Code Playgroud)