Starting with a dataframe:
val someDF = Seq(
  (8, "bat", "h"),
  (64, "mouse", "t"),
  (-27, "horse", "x")
).toDF("number", "thing", "letter")
someDF.show()
+------+-----+------+
|number|thing|letter|
+------+-----+------+
|     8|  bat|     h|
|    64|mouse|     t|
|   -27|horse|     x|
+------+-----+------+
and a Map:
val lookup = Map(
  "number" -> "id",
  "thing" -> "animal"
)
I'd like to select and rename the columns such that number becomes id, thing becomes animal and so on.
The renaming is covered in another Stack Overflow question: Renaming column names of a DataFrame in Spark Scala, I'm sure there is a straightforward way to do the select at the same time that I'm not seeing.
I thought something along these lines would work, but get lots of type mismatches despite the input is a string and it works with a Seq instead of map:
val renamed_selected = someDF.select(
      lookup.map(m => col(m._1).as(m._2))
    ):_*
So the desired output is:
+------+------+
|id    |animal|
+------+------+
|     8|  bat |     
|    64|mouse |     
|   -27|horse |     
+------+------+
Thanks
Clarification on duplicate question flag: The question Renaming column names of a DataFrame in Spark Scala does not cover how to rename and select columns at the same time.