【问题标题】:How to get Start and End date in PySpark?如何在 PySpark 中获取开始和结束日期?
【发布时间】:2020-02-24 15:53:08
【问题描述】:

我在下面有一个 Spark 数据框(articleDF1),我正在尝试使用 Date 列向数据框添加两列开始日期和结束日期,并按 post_evar10 对结果数据框进行分组。 最终的 Dataframe 将具有 post_evar10、开始日期和结束日期

 -------+--------------------+
|      Date|         post_evar10|
+----------+--------------------+
|2019-09-02|www:/espanol/recu...|
|2019-09-02|www:/caregiving/h...|
|2019-12-15|www:/health/condi...|
|2019-09-01|www:/caregiving/h...|
|2019-08-31|www:/travel/trave...|
|2020-01-20|www:/home-family/...|

我尝试过的:

from pyspark.sql import functions as f
articleDF3 = articleDF1.withColumn('Start_Date', f.min(f.col('Date'))).withColumn('Start_Date', f.max(f.col('Date'))).groupBy(f.col("post_evar10")).drop("Date")

出现错误: org.apache.spark.sql.AnalysisException:分组表达式序列为空,并且“temp.ms_article_lifespan_final.Date”不是聚合函数。如果您不关心,请将 '(min(temp.ms_article_lifespan_final.Date) AS Start_Date)' 包装在窗口函数中或将 'temp.ms_article_lifespan_final.Date' 包装在 first() (或 first_value)中你得到哪个值。;;

【问题讨论】:

    标签: python apache-spark pyspark databricks pyspark-dataframes


    【解决方案1】:

    这是你预期的结果吗?

    要获取每行的最小值,最大值,我们可以使用 window 函数并获取 min,max 然后分组并在聚合中获取最小值,最大值!

    Example:

    import sys
    from pyspark.sql.window import Window
    from pyspark.sql.functions import *
    
    #Sample data
    df=sc.parallelize([('2019-09-02','www:/espanol/r'),('2019-09-02','www:/caregiving/h'),('2019-12-15','www:/health/condi')]).toDF(['Date','post_evar10']).withColumn("Date",col("Date").cast("Date"))
    
    #window on all rows
    w = Window.orderBy("Date").rowsBetween(-sys.maxsize, sys.maxsize)
    #or
    w = Window.orderBy("Date").rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)
    
    df.withColumn("min_Date",min("Date").over(w)).\ #get min value for Date
    withColumn("max_Date",max("Date").over(w)).\ #get max value for Date
    groupBy("post_evar10").\ #groupby on post_evar10
    agg(min("min_Date").alias("Start_date"),max("max_Date").alias("End_date")).\ #get min,max
    show()
    
    #+-----------------+----------+----------+
    #|      post_evar10|Start_date|  End_date|
    #+-----------------+----------+----------+
    #|   www:/espanol/r|2019-09-02|2019-12-15|
    #|www:/caregiving/h|2019-09-02|2019-12-15|
    #|www:/health/condi|2019-09-02|2019-12-15|
    #+-----------------+----------+----------+
    

    (或)

    By using first,last functions over window:

    df.withColumn("min_Date",first("Date").over(w)).\
    withColumn("max_Date",last("Date").over(w)).\
    groupBy("post_evar10").\
    agg(min("min_Date").alias("Start_date"),max("max_Date").alias("End_date")).\
    show()
    

    Generate min,max for each post_evar10 unique value:

    w = Window.partitionBy('post_evar10').orderBy("Date").rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)
    
    df=sc.parallelize([('2019-09-02','www:/espanol/r'),('2019-09-02','www:/caregiving/h'),('2019-09-03','www:/caregiving/h'),('2019-12-15','www:/health/condi')]).toDF(['Date','post_evar10']).withColumn("Date",col("Date").cast("Date"))
    
    df.groupBy("post_evar10").\
    agg(min("Date").alias("Start_date"),max("Date").alias("End_date")).\
    show()
    
    #+-----------------+----------+----------+
    #|      post_evar10|Start_date|  End_date|
    #+-----------------+----------+----------+
    #|www:/health/condi|2019-12-15|2019-12-15|
    #|   www:/espanol/r|2019-09-02|2019-09-02|
    #|www:/caregiving/h|2019-09-02|2019-09-03|
    #+-----------------+----------+----------+
    

    【讨论】:

    • 我用 Windows 函数尝试了类似的东西,见下面的代码,但没有使用 sys 函数。在这里使用 sys 函数有什么好处。是的,生成的 DF 将包含每个 post_evar10 的开始和结束日期。
    • from pyspark.sql.window import Window from pyspark.sql import functions as f articleDF1.window = Window.partitionBy('post_evar10').orderBy('Date') articleDF3 = articleDF1.withColumn(' Start_Date', f.min(f.col('Date')).over(articleDF1.window)).withColumn('End_Date', f.max(f.col('Date')).over(articleDF1.window )).drop("日期") 文章DF3.show()
    • 我们可以使用sys(或)Window.unboundedPreceding 在逻辑偏移上创建行框。在我们的例子中,我们有完整的记录作为我们的行框架并计算最小值、最大值! databricks.com/blog/2015/07/15/…stackoverflow.com/questions/48138632/…
    猜你喜欢
    • 2021-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-13
    • 2022-01-23
    相关资源
    最近更新 更多