How to rename a column in a spark scala dataframe after checking if it's present or not?

Viewed 52

I need to rename a column, "DOB" in my spark dataframe to "DateOfBirth" after checking if the column is available in the dataframe or not.

I am doing the below for renaming the column, but how do I insert the condition here

df.withColumnRenamed("DOB","DateOfBirth")

Could anyone please help on this.

1 Answers

You can just check if the column exists with the column() function and only rename the column in that case:

val renamed_df = 
    if (df.columns.contains("DOB"))
        df.withColumnRenamed("DOB","DateOfBirth")
    else df
Related