Skip to content
Advertisement

Pivotting DataFrame with fixed column names

Let’s say I have below dataframe:

JavaScript

and by design each user has 3 rows. I want to turn my DataFrame into:

JavaScript

I was trying to groupBy(col('user')) and then pivot by ticker but it returns as many columns as different tickers there are so instead I wish I could have fixed number of columns. Is there any other Spark operator I could use for that?

Using PySpark and Azure Databricks.

Advertisement

Answer

If the order doesn’t matter, then you can use row_number to number them

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement