I have a DataFrame
that looks like this:
+-----------------+-------+
|Id | value |
+-----------------+-------+
| 1622| 139685|
| 1622| 182118|
| 1622| 127955|
| 3837|3224815|
| 1622| 727761|
| 1622| 155875|
| 3837|1504923|
| 1622| 139684|
+-----------------+-------+
And I want to turn it into:
+-----------------+-------------------------------------------+
|Id | value |
+-----------------+-------------------------------------------+
| 1622|139685,182118,127955,727761,155875,139684 |
| 3837|3224815,1504923 |
+-----------------+-------------------------------------------+
Is this possible with DataFrame
functions only or do I need to convert it to and RDD
?
It is possible with the DataFrame
API. Try:
df.groupBy(col("Id"))
.agg(collect_list(col("value")) as "value")
If instead of an Array
you want a String
separated by ,
, then try this:
df.groupBy(col("Id"))
.agg(collect_list(col("value")) as "value")
.withColumn("value", concat_ws(",", col("value")))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With