-1

I have a dataframe nameDF as below:

scala> val nameDF = Seq(("John","A"), ("John","B"), ("John","C"), ("John","D"), ("Bravo","E"), ("Bravo","F"), ("Bravo","G")).toDF("Name","Init")
nameDF: org.apache.spark.sql.DataFrame = [Name: string, Init: string]

scala> nameDF.show
+------+----+
|Name  |Init|
+------+----+
|Johnny|   A|
|Johnny|   B|
|Johnny|   C|
|Johnny|   D|
|Bravo |   E|
|Bravo |   F|
|Bravo |   G|
+------+----+

Without using SQL, I am trying to group the names and convert the multiple rows of each "Name" into a single row as given below:

+------+-------+
|Name  |Init   |
+------+-------+
|Johnny|A,B,C,D|
|Bravo |E,F,G  |
+------+-------+

I see the available options to pivot are not suitable for String operations. enter image description here

Is Pivot the correct option in this case ? If not, could anyone let me know how can I achieve the solution ?

1

1 Answer 1

2

Try this:

import org.apache.spark.sql.functions._

df.groupBy($"Name")
 .agg(concat_ws(",", sort_array(collect_list($"Init"))).as("Init"))
Sign up to request clarification or add additional context in comments.

1 Comment

Raphael, Your solution works but I have a doubt here. AGG is an aggregate function. How is it working on a column that has String content ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.