Q:

How to rename column headers in a scala dataframe

How can I do string.replace("fromstr", "tostr") on a scala dataframe.
As far as I can see withColumnRenamed performs replace on all columns and not just the headers.

A:

withColumnRenamed renames column names only, data remains the same. If you need to change rows context, you can use one of the following:
import sparkSession.implicits._
import org.apache.spark.sql.functions._

val inputDf = Seq("to_be", "misc").toDF("c1")
val resultd1Df = inputDf
  .withColumn("c2", regexp_replace($"c1", "^to_be$", "not_to_be"))
  .select($"c2".as("c1"))
resultd1Df.show()

val resultd2Df = inputDf
  .withColumn("c2", when($"c1" === "to_be", "not_to_be").otherwise($"c1"))
  .select($"c2".as("c1"))
resultd2Df.show()

def replace(mapping: Map[String, String]) = udf(
  (from: String) => mapping.get(from).orElse(Some(from))
)

val resultd3Df = inputDf
  .withColumn("c2", replace(Map("to_be" -> "not_to_be"))($"c1"))
  .select($"c2".as("c1"))
resultd3Df.show()

Input dataframe:
+-----+
|   c1|
+-----+
|to_be|
| misc|
+-----+

Result dataframe:
+---------+
|       c1|
+---------+
|not_to_be|
|     misc|
+---------+

You can find the list of available Spark functions there 

