Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transposing a Spark DataFrame from row to column in PySpark and appending it with another DataFrame

I have a Spark DataFrame in PySpark avg_length_df that looks like -

+----------------+---------+----------+-----------+---------+-------------+----------+
|       id       |        x|         a|          b|        c|      country|     param|
+----------------+---------+----------+-----------+---------+-------------+----------+
|            40.0|      9.0|     5.284|      5.047|    6.405|         13.0|avg_length|
+----------------+---------+----------+-----------+---------+-------------+----------+

I want to transpose it from row to column so that it becomes -

+----------+
|avg_length|
+----------+
|      40.0|
|       9.0|
|     5.284|
|     5.047|
|     6.405|
|      13.0|
+----------+

Next, I have a second DataFrame df2:

+----------------+------+
|       col_names|dtypes|
+----------------+------+
|              id|string|
|               x|   int|
|               a|string|
|               b|string|
|               c|string|
|         country|string|
+----------------+------+

I want to create a column avg_length in df2 the equals to the transposed DataFrame above. So the expected output would look like:

+----------------+------+----------+
|       col_names|dtypes|avg_length|
+----------------+------+----------+
|              id|string|      40.0|
|               x|   int|       9.0|
|               a|string|     5.284|
|               b|string|     5.047|
|               c|string|     6.405|
|         country|string|      13.0|
+----------------+------+----------+

How do I complete the 2 operations?

like image 316
K. K. Avatar asked Oct 22 '25 19:10

K. K.


1 Answers

>>> from pyspark.sql import *
#Input DataFrame
>>> df.show()
+----+---+-----+-----+-----+-------+----------+
|  id|  x|    a|    b|    c|country|     param|
+----+---+-----+-----+-----+-------+----------+
|40.0|9.0|5.284|5.047|6.405|   13.0|avg_length|
+----+---+-----+-----+-----+-------+----------+

>>> avgDF  = df.groupBy(df["id"],df["x"],df["a"],df["b"],df["c"],df["country"]).pivot("param").agg(concat_ws("",collect_list(to_json(struct("id","x","a","b","c","country"))))).drop("id","x","a","b","c","country")
>>> avgDF.show(2,False)
+----------------------------------------------------------------------------+
|avg_length                                                                  |
+----------------------------------------------------------------------------+
|{"id":"40.0","x":"9.0","a":"5.284","b":"5.047","c":"6.405","country":"13.0"}|
+----------------------------------------------------------------------------+

>>> finalDF = avgDF.withColumn("value", explode(split(regexp_replace(col("avg_length"),"""[\\{ " \\}]""",""),","))).withColumn("avg_length", split(col("value"), ":")[1]).withColumn("col_names", split(col("value"), ":")[0]).drop("value")
>>> finalDF.show(10,False)
+----------+---------+
|avg_length|col_names|
+----------+---------+
|40.0      |id       |
|9.0       |x        |
|5.284     |a        |
|5.047     |b        |
|6.405     |c        |
|13.0      |country  |
+----------+---------+

#other dataframe
>>> df2.show()
+---------+------+
|col_names|dtypes|
+---------+------+
|       id|string|
|        x|   int|
|        a|string|
|        b|string|
|        c|string|
|  country|string|
+---------+------+

>>> df2.join(finalDF,"col_names").show(10,False)
+---------+------+----------+
|col_names|dtypes|avg_length|
+---------+------+----------+
|id       |string|40.0      |
|x        |int   |9.0       |
|a        |string|5.284     |
|b        |string|5.047     |
|c        |string|6.405     |
|country  |string|13.0      |
+---------+------+----------+
like image 135
Nikhil Suthar Avatar answered Oct 25 '25 08:10

Nikhil Suthar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!