0

How to convert rows into string values in Apache Spark

I have a spark dataframe like this:

fruit | name
--------------
fruit | apple
fruit | orange
fruit | mango

I want to convert it into this:

fruit | string
----------------------------
fruit | apple, orange, mango

How can I achieve this in Apache Spark?

2

1 Answer 1

2

Look at collect_list

df.groupBy("fruit").agg(collect_list("name"))

it will group values and create array of them as a new column.

If you want to have string, please see this question (thanks @mtoto)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.