1

How could I duplicate row according to this

|source_ip  |dest_ip |source_port|dest_port|
|192.168.1.1|10.0.0.1|5343       |22       |

Into

|ip         |source_port|dest_port|
|192.168.1.1|5343       |22       |
|10.0.0.1   |5343       |22       |

Using pyspark?

0

1 Answer 1

1

Try with array and explode.

Example:

df.show()
#+-----------+--------+-----------+---------+
#|         ip| dest_ip|source_port|dest_port|
#+-----------+--------+-----------+---------+
#|192.168.1.1|10.0.0.1|       5343|       22|
#+-----------+--------+-----------+---------+

df.withColumn("arr",array(col("ip"),col("dest_ip"))).\
selectExpr("explode(arr) as ip","source_port","dest_port").\
show()
#+-----------+-----------+---------+
#|         ip|source_port|dest_port|
#+-----------+-----------+---------+
#|192.168.1.1|       5343|       22|
#|   10.0.0.1|       5343|       22|
#+-----------+-----------+---------+
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.