如何在pyspark中添加/包括透视表中的标题行和总计行?

如何在pyspark中添加/包括透视表中的标题行和总计行?,pyspark,openpyxl,Pyspark,Openpyxl,我正在通过PySpark将数据导出到Excel。我有一个数据集 df_raw = spark.createDataFrame([("2015-10", 'U.S.', 500), \ ("2018-10", 'Germany', 580), \ ("2019-08", 'Japan', 230), \ ("

我正在通过PySpark将数据导出到Excel。我有一个数据集

df_raw = spark.createDataFrame([("2015-10", 'U.S.', 500), \
                                ("2018-10", 'Germany', 580), \
                                ("2019-08", 'Japan', 230), \
                                ("2015-12", 'U.S.', 500), \
                                ("2015-11", 'Germany', 580), \
                                ("2015-12", 'Japan', 502), \
                                ("2018-10", 'U.S.', 520), \
                                ("2019-08", 'Canada', 200)]).toDF("ym", "country", "points")
+-------+-------+------+
|     ym|country|points|
+-------+-------+------+
|2015-10|   U.S.|   500|
|2018-10|Germany|   580|
|2019-08|  Japan|   230|
|2015-12|   U.S.|   500|
|2015-11|Germany|   580|
|2015-12|  Japan|   502|
|2018-10|   U.S.|   520|
|2019-08| Canada|   200|
+-------+-------+------+
我将其转换为透视表

df_pivot = df_raw.groupBy('country').pivot("ym").sum('points')
+-------+-------+-------+-------+-------+-------+
|country|2015-10|2015-11|2015-12|2018-10|2019-08|
+-------+-------+-------+-------+-------+-------+
|Germany|   null|    580|   null|    580|   null|
|   U.S.|    500|   null|    500|    520|   null|
| Canada|   null|   null|   null|   null|    200|
|  Japan|   null|   null|    502|   null|    230|
+-------+-------+-------+-------+-------+-------+
我想通过
Openpyxl
将带有标题行和总计行的表格导出到Excel电子表格中

我可以使用
.collect()
循环数据帧,并将记录附加到工作表中,但它不包括标题,我还想添加一个总计行

总计行的示例:

+-------+-------+-------+-------+-------+-------+
|country|2015-10|2015-11|2015-12|2018-10|2019-08|
+-------+-------+-------+-------+-------+-------+
|Germany|   null|    580|   null|    580|   null|
|   U.S.|    500|   null|    500|    520|   null|
| Canada|   null|   null|   null|   null|    200|
|  Japan|   null|   null|    502|   null|    230|
+-------+-------+-------+-------+-------+-------+
|       |    500|    580|   1002|   1100|    430|
+-------+-------+-------+-------+-------+-------+

如何实现这一点?

尝试查看
汇总功能,然后将其合并,例如

df = df_raw.groupBy('country').pivot("ym").sum('points')
df2 = df.rollup('country').count()
或者,只需获取pivot的输出,动态选择日期列(在regex模式或其他模式上),并使用
sum()
将其聚合,然后将别名返回到列名中

编辑: 现在我明白你到底想要什么了。我仍然会使用
汇总
,但会结合一些重命名和联合,例如:

from functools import reduce

agg_cols = df_pivot.columns[1:]
rollup_df = df_pivot.rollup().sum()

renamed_df = reduce(
    lambda rollup_df, idx: rollup_df.withColumnRenamed(rollup_df.columns[idx], agg_cols[idx]), 
    range(len(rollup_df.columns)), rollup_df
)

renamed_df = renamed_df.withColumn('country', f.lit('Total'))

df_pivot.unionByName(
    renamed_df
).show()
输出:

+-------+-------+-------+-------+-------+-------+
|country|2015-10|2015-11|2015-12|2018-10|2019-08|
+-------+-------+-------+-------+-------+-------+
|Germany|   null|    580|   null|    580|   null|
|   U.S.|    500|   null|    500|    520|   null|
| Canada|   null|   null|   null|   null|    200|
|  Japan|   null|   null|    502|   null|    230|
|  Total|    500|    580|   1002|   1100|    430|
+-------+-------+-------+-------+-------+-------+
在PySpark 2.4.3上测试

rollup()
似乎只适用于从左到右运行的计数。在导出到列表时,我希望得到每列的总和以及包含标题行。我明白了。仍然使用
rollup()
,但将其与一些重命名和
unionByName()
一起使用。我修改了原始答案以包含代码。