【发布时间】:2021-09-26 21:43:43
【问题描述】:
我在下面有这个数据框,我想要实现的是,只有当一行有一个“Y”标志时,code 列中的值将被聚合到一个列表。我已经尝试过 sql 打击,但没有奏效。这个怎么做?我在下面的代码和示例输出中进行了评论。非常感谢您的帮助。
输入:
name code flag
big bird A Y
elmo B N
cookie monster C Y
cookie monster D N
预期输出:
name hasYflag Codelist
big bird Y A.
elmo N //elmo does not have codelist as the flag is N
cookie monster Y C,D. //cookie monster has codelist as there is one Y (row 3 above) flag
我尝试过这样做,但它不起作用。我希望用 spark sql 而不是 Spark sql api 来做到这一点:
select name,
case when max(flag) = "Y" then "Y" else "N" end as hasYflag
case when max(flag) = "Y" then sort_array(collect_set(code)) else null as Codelist
from df
groupby name
【问题讨论】:
标签: sql dataframe apache-spark apache-spark-sql dataset