首页 > 解决方案 > 如何将我的输出作为每次运行的新行插入

问题描述

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

df = 
+-------------+---------------+-----------+
date            delivery            Value
+-------------+---------------+-----------+
01/01/2018        yes               0
02/01/2018        no                3
03/01/2018        yes               3
04/01/2018        no                0
01/02/2018        yes               3
02/02/2018        yes               0
03/02/2018        yes               0
04/02/2018        yes               2
01/03/2018        no                0 
02/03/2018        yes               0
03/03/2018        no                3
04/03/2018        no                2

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

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

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

+-------------+---------------+----------------------+----------------------+
date            delivery           valuewithzero          percentage        
+-------------+---------------+----------------------+----------------------+
19/2021             yes                 4                     33.3%
19/2021             no                  2                     16.6%
20/2021             yes                 4                     33.3%
20/2021             no                  2                     16.6%
21/2021             yes                 4                     33.3%
21/2021             no                  2                     16.6%

标签: apache-sparkpysparkapache-spark-sqlpyspark-dataframes

解决方案


推荐阅读