【问题标题】:pyspark sum group by month and date using start and end datepyspark sum group by month and date using start and end date
【发布时间】:2020-07-21 23:11:28
【问题描述】:

我有一个合同数据框:开始期、结束期、每月价格。我正在尝试获取每个月的总收入。

当前数据框:

id      start_period   end_period     montly_price
1       2019-01-01     2019-03-01     30
2       2019-01-01     2019-06-01     20
3       2019-07-01     2020-08-01     10
4       2019-07-01     2020-08-01     10

预期输出:

period          revenue_per_month_all_ids
2019-01         50
2019-02         50
2019-04         20
2019-05         20
2019-06         0
2019-07         20

【问题讨论】:

    标签: pyspark


    【解决方案1】:

    假设您没有合同,其 start_period 和 end_period 的月份不同(如果是这种情况,您希望如何继续?您可以编辑您的问题或评论这篇文章吗?)

    
    import pyspark.sql.functions as psf
    
    df.withColumn("month",psf.month('start_period'))
      .groupBy("period")
      .agg(psf.sum("monthly_price").alias("revenue_per_month_all_ids"))
    

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2017-12-07
      • 2020-01-19
      • 2022-12-02
      • 1970-01-01
      • 2017-08-08
      • 2014-06-29
      相关资源
      最近更新 更多