如何透视Spark数据框表? [英] how to pivot Spark dataframe table?

查看:60
本文介绍了如何透视Spark数据框表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3列的表格:

+---+----+----+
| id|type| val|
+---+----+----+
|  1|   A|   0|
|  2|   A|   0|
|  4|   A|   0|
|  2|   B|   1|
|  4|   B|   1|
+---+----+----+

,我想将其转换为类似的内容:

and I would like to transforme it to something like:

+---+----+----+
|   |   A|   B|
+---+----+----+
|  1|   0|   -|
|  2|   1|   1|
|  4|   0|   1|
+---+----+----+

我尝试了这个,但是没有用:

I tried this but didn't work:

val data_array = data.pivot(cols=['type'],rows=['id'],values='val')

推荐答案

df.groupBy("id").pivot("type").agg(first("value")).na.fill("-").show

df是从测试数据文件创建的数据框

df is the dataframe created from the test data file

这篇关于如何透视Spark数据框表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆