如何在每次运行时将输出作为新行插入

kb5ga3dv  于 2021-07-13  发布在  Spark
关注(0)|答案(0)|浏览(262)

我有一个类似于下面的数据集

  1. df =
  2. +-------------+---------------+-----------+
  3. date delivery Value
  4. +-------------+---------------+-----------+
  5. 01/01/2018 yes 0
  6. 02/01/2018 no 3
  7. 03/01/2018 yes 3
  8. 04/01/2018 no 0
  9. 01/02/2018 yes 3
  10. 02/02/2018 yes 0
  11. 03/02/2018 yes 0
  12. 04/02/2018 yes 2
  13. 01/03/2018 no 0
  14. 02/03/2018 yes 0
  15. 03/03/2018 no 3
  16. 04/03/2018 no 2

我总是尝试在每次运行时插入代码的输出,并将当前标记作为新行。目前我尝试:

  1. total = df.count()
  2. df2 = df.filter(df.Value==0).groupBy("delivery")\
  3. .count()\
  4. .withColumn("percent",(F.col('cnt_grp')/total)*100) \
  5. .withColumn("date",current_timestamp())

但是每次我运行这个我只得到两行,而不是每次运行两个新行。我期望的输出应该类似于

  1. +-------------+---------------+----------------------+----------------------+
  2. date delivery valuewithzero percentage
  3. +-------------+---------------+----------------------+----------------------+
  4. 19/2021 yes 4 33.3%
  5. 19/2021 no 2 16.6%
  6. 20/2021 yes 4 33.3%
  7. 20/2021 no 2 16.6%
  8. 21/2021 yes 4 33.3%
  9. 21/2021 no 2 16.6%

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题