2

I have dataframes with one row:

A B C D E
4 1 7 2 3

I would like to convert this to a dataframe with the following format:

Letter Number
A      4
B      1
C      7
D      2
E      3
Outlier
  • 417
  • 2
  • 10
  • 1
    Does this answer your question? [Unpivot in spark-sql/pyspark](https://stackoverflow.com/questions/42465568/unpivot-in-spark-sql-pyspark) – Kafels Oct 07 '21 at 12:53

1 Answers1

0

I did not find any built-in pyspark function in the docs, so I created a very simple basic function that does the job. Given that your dataframe df has only one row, you can use the following solution.

def my_transpose(df):
  
  # get values
  letter = df.columns
  number = list(df.take(1)[0].asDict().values())
  
  # combine values for a new Spark dataframe
  data = [[a, b] for a, b in zip(letter, number)]
  
  res = spark.createDataFrame(data, ['Letter', 'Number'])
  return res



my_transpose(df).show()

+------+------+
|Letter|Number|
+------+------+
|     A|     4|
|     B|     1|
|     C|     7|
|     D|     2|
|     E|     3|
+------+------+
Ric S
  • 9,073
  • 3
  • 25
  • 51