I\'ve successfully create a row_number()
partitionBy
by in Spark using Window, but would like to sort this by descending, instead of the default ascend
Update Actually, I tried looking more into this, and it appears to not work. (in fact it throws an error). The reason why it didn't work is that I had this code under a call to display()
in Databricks (code after the display()
call is never run). It seems like the orderBy()
on a dataframe and the orderBy()
on a window
are not actually the same. I will keep this answer up just for negative confirmation
As of PySpark 2.4,(and probably earlier), simply adding in the keyword ascending=False
into the orderBy
call works for me.
Ex.
personal_recos.withColumn("row_number", F.row_number().over(Window.partitionBy("COLLECTOR_NUMBER").orderBy("count", ascending=False)))
and
personal_recos.withColumn("row_number", F.row_number().over(Window.partitionBy("COLLECTOR_NUMBER").orderBy(F.col("count").desc())))
seem to give me the same behaviour.