Have my dataframe as shown below.Here I have to remove the last occurrence of the string "_value" from all the column name of my dataframe.
import spark.implicits._
import org.apache.spark.sql.functions._
val simpledata = Seq(("file1","name1","101"),
("file1","name1","101"),
("file1","name1","101"),
("file1","name1","101"),
("file1","name1","101"))
val df = simpledata.toDF("filename_value","name_value_value","serialNo_value")
df.show()
Output menu
enter image description here
If I use replaceAll:
val renamedColumnsDf = df.columns.map(c => df(c).as(c.replaceAll('_value',""))) it removes all the _values but i need only to remove the string based on last occurance.
Need help here to remove the string based on occurrence in column name.
My output should be:
+--------------+----------------+--------------+
|filename |name_value |serialNo |
+--------------+----------------+--------------+
| file1| name1| 101|
| file1| name1| 101|
| file1| name1| 101|
| file1| name1| 101|
| file1| name1| 101|
+--------------+----------------+--------------+