3

How can I reverse this DataFrame using Scala. I saw sort functions but must specific column, I only want to reverse them

+---+--------+-----+
|id |    name|note |
+---+--------+-----+
|1  | james  |any  |
|3  | marry  |some |
|2  | john   |some |
|5  | tom    |any  |
+---+--------+-----+

to:

+---+--------+-----+
|id |    name|note |
+---+--------+-----+
|5  | tom    |any  |
|2  | john   |some |
|3  | marry  |some |
|1  | james  |any  |
+---+--------+-----+
Jacek Laskowski
  • 72,696
  • 27
  • 242
  • 420
MasterLuV
  • 396
  • 1
  • 17
  • Do note that in most cases there is no guranteed order of the dataframe which is why you won't find any operation such as reverse (if you sort by a column it's no problem however). See e.g.: https://issues.apache.org/jira/browse/SPARK-16207 – Shaido Jul 30 '19 at 09:04

2 Answers2

6

You can add a column with increasing id with use of monotonically_increasing_id() and sort in descending order

val dff = Seq(
  (1, "james", "any"),
  (3, "marry", "some"),
  (2, "john", "some"),
  (5, "tom", "any")
).toDF("id", "name", "note")

dff.withColumn("index", monotonically_increasing_id())
  .sort($"index".desc)
  .drop($"index")
  .show(false)

Output:

+---+-----+----+
|id |name |note|
+---+-----+----+
|5  |tom  |any |
|2  |john |some|
|3  |marry|some|
|1  |james|any |
+---+-----+----+
koiralo
  • 22,594
  • 6
  • 51
  • 72
-1

You could do something like this:

val reverseDf = df.withColumn("row_num", row_number.over(Window.partitionBy(lit(1)).orderBy(lit(1))))
                .orderBy($"row_num".desc)
                .drop("row_num")

Or refer this instead of row number.

wypul
  • 807
  • 6
  • 9