我有DF测试包含以下列
Type Name Country Year Value
1 Rec US 2018 8
2 fg UK 2019 2
5 vd India 2020 1
7 se US 2021 3
我想在它上面做一个支点,我试过下面的表达
pivotdata=spark.sql("select * from test").groupby("Country").pivot("Year").sum("Value").show()
我得到了输出,但除了剩下的两列之外,它只显示了几列
Country 2018 2019 2020 2021
US - -
UK - -
India - -
US - -
那么,如果我想要所有列,我们该怎么办
如果我正确理解了您需要的内容,您必须在sum()中提供其他列。考虑下面的例子:
如果您不喜欢有趣的列名,那么可以使用agg函数为数据透视列名定义自己的后缀
相关问题 更多 >
编程相关推荐