2

How do I transpose columns in Pyspark? I want to make columns become rows, and rows become the columns.

Here is the input:

+---- +------+-----+-----+
|idx  | vin  |cur  | mean|
+---- +------+-----+-----+
|Type1|     D| 5.0 |6.0  |
|Type2|     C| null| 7.0 |
+---- +------+-----+-----+ 

Expected Outcome:

+---- +------+-----+
|idx  |Type1 |Type2| 
+---- +------+-----+
|vin  |   D  | C   |
|cur  |  5.0 | null| 
|mean |  6.0 | 7.0 |     
+-----+------+-----+
5
  • Please repeat on topic and how to ask from the intro tour. Asking a question that you haven't researched is not acceptable. Commented Feb 9, 2021 at 22:26
  • 3
    Actually, this appears to be a decent question. Commented Feb 9, 2021 at 22:31
  • 2
    @Prune lack of research is a downvote reason, sure, but not a close vote reason. OP wants to know how to complete this functionality in pyspark, not pandas. I've voted to reopen based on that discrepancy. Commented Feb 9, 2021 at 23:06
  • @inses0606 Have you made an attempt at this yourself and come across a problem? If not, that's your recommended first step. Then you can explain in the question exactly how your own attempt comes up short. Commented Feb 9, 2021 at 23:07
  • Thanks for the correction. My apologies. Commented Feb 9, 2021 at 23:37

1 Answer 1

2

You can combine stack function to unpivot vin, mean and cur columns then pivot column idx:

from pyspark.sql import functions as F

df1 = df.selectExpr("idx", "stack(3, 'vin',vin, 'cur',cur, 'mean',mean)") \
    .select("idx", "col0", "col1") \
    .groupBy("col0") \
    .pivot("idx").agg(F.first("col1")) \
    .withColumnRenamed("col0", "idx")

df1.show(truncate=False)

#+----+-----+-----+
#|idx |Type1|Type2|
#+----+-----+-----+
#|vin |D    |C    |
#|mean|6.0  |7.0  |
#|cur |5.0  |null |
#+----+-----+-----+

You apply the transformation one by one to see how it works and what do each part.

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.