Expand Column to many rows in Scala

Mohd Zoubi

I have the following dataframe (df2)

+----------------+---------+-----+------+-----+
|         Colours| Model   |year |type  |count|
+----------------+---------+-----+------+-----|
|red,green,white |Mitsubishi|2006|sedan |3    |
|gray,silver     |Mazda    |2010 |SUV   |2    |
+----------------+---------+-----+------+-----+

I need to explode the column "Colours", so it looks an expanded column like this:

+----------------+---------+-----+------+
|         Colours| Model   |year |type  |
+----------------+---------+-----+------+
|red             |Mitsubishi|2006|sedan |
|green           |Mitsubishi|2006|sedan |
|white           |Mitsubishi|2006|sedan |
|gray            |Mazda    |2010 |SUV   |
|silver          |Mazda    |2010 |SUV   |
+----------------+---------+-----+------+

I have created an array

val colrs=df2.select("Colours").collect.map(_.getString(0))

and added the array to dataframe

val cars=df2.withColumn("c",explode($"colrs")).select("Colours","Model","year","type")

but it didn't work, any help please.

Ramesh Maharjan

You can use split and explode functions as below in your dataframe (df2)

import org.apache.spark.sql.functions._
val cars = df2.withColumn("Colours", explode(split(col("Colours"), ","))).select("Colours","Model","year","type")

You will have output as

cars.show(false)

+-------+----------+----+-----+
|Colours|Model     |year|type |
+-------+----------+----+-----+
|red    |Mitsubishi|2006|sedan|
|green  |Mitsubishi|2006|sedan|
|white  |Mitsubishi|2006|sedan|
|gray   |Mazda     |2010|SUV  |
|silver |Mazda     |2010|SUV  |
+-------+----------+----+-----+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to expand a list column in scala to multiple rows

From Dev

Expand a column holding iterables into rows

From Dev

Postgres: Expand JSON column into rows

From Dev

Expand rows by column value in Presto

From Dev

Pandas: expand list in column to distinct rows

From Dev

Expand pandas DataFrame column into multiple rows

From Dev

Expand number of rows by column count VBA

From Dev

Expand table rows based on column value

From Dev

Applying a Function to Expand a Single Row to Many Rows by Group in R

From Dev

Pandas parse json in column and expand to new rows in dataframe

From Dev

Pandas parse json in column and expand to new rows in dataframe

From Dev

Convert many column into multiple columns and rows in mysql

From Dev

Count how many rows that have a special value in a column

From Dev

After how many rows is temp table column size determined?

From Dev

Python: Taking mean over many rows and fixed column

From Dev

MySQL update just day in date column in many rows

From Dev

Show how many rows data has moved in relation to adjacent column

From Dev

R: Expand dataframe rows

From Dev

Expand a UICollectionView in the middle of rows

From Dev

$('textarea').rows expand/contract

From Dev

$('textarea').rows expand/contract

From Dev

Merge Spark dataframe rows based on key column in Scala

From Dev

How to get these rows from many to many table, which contains 0 in exact column?

From Dev

Expand Pandas Group By to Many Columns

From Dev

ArrayFormula expand to a whole column

From Dev

convert multi rows into multi rows with many Column and name from the first table

From Dev

database design - Should I have many column about uploaded image column or I should put it in new table with many rows?

From Dev

How many rows where column > value SINCE last matching row where column = value

From Dev

Expand a single row into multiple rows

Related Related

  1. 1

    How to expand a list column in scala to multiple rows

  2. 2

    Expand a column holding iterables into rows

  3. 3

    Postgres: Expand JSON column into rows

  4. 4

    Expand rows by column value in Presto

  5. 5

    Pandas: expand list in column to distinct rows

  6. 6

    Expand pandas DataFrame column into multiple rows

  7. 7

    Expand number of rows by column count VBA

  8. 8

    Expand table rows based on column value

  9. 9

    Applying a Function to Expand a Single Row to Many Rows by Group in R

  10. 10

    Pandas parse json in column and expand to new rows in dataframe

  11. 11

    Pandas parse json in column and expand to new rows in dataframe

  12. 12

    Convert many column into multiple columns and rows in mysql

  13. 13

    Count how many rows that have a special value in a column

  14. 14

    After how many rows is temp table column size determined?

  15. 15

    Python: Taking mean over many rows and fixed column

  16. 16

    MySQL update just day in date column in many rows

  17. 17

    Show how many rows data has moved in relation to adjacent column

  18. 18

    R: Expand dataframe rows

  19. 19

    Expand a UICollectionView in the middle of rows

  20. 20

    $('textarea').rows expand/contract

  21. 21

    $('textarea').rows expand/contract

  22. 22

    Merge Spark dataframe rows based on key column in Scala

  23. 23

    How to get these rows from many to many table, which contains 0 in exact column?

  24. 24

    Expand Pandas Group By to Many Columns

  25. 25

    ArrayFormula expand to a whole column

  26. 26

    convert multi rows into multi rows with many Column and name from the first table

  27. 27

    database design - Should I have many column about uploaded image column or I should put it in new table with many rows?

  28. 28

    How many rows where column > value SINCE last matching row where column = value

  29. 29

    Expand a single row into multiple rows

HotTag

Archive