Home > Mobile >  Trying to get Scala to rename a column, and it does not work
Trying to get Scala to rename a column, and it does not work

Time:12-04

val mLookup = vistaRepro.GetAll().toDF().withColumnRenamed("ID_Individuals", "pii") mLookup.show()

Why does show still return a recordset with ID_Individuals as name?

CodePudding user response:

Do you have that column "ID_Individuals". Please verify using below command:

val mLookup = vistaRepro.GetAll().toDF()
mLookup.show

By default if it is single value then column would be "value" otherwise _1,_2 and so on

CodePudding user response:

That withColumnRenamed is a no-op if schema doesn't contain column "ID_Individuals".

First of all know the df schema:

df.printSchema()

or validate programmatically that column exists:

def colExists(df: DataFrame, targetCol: String): Boolean =
  df
    .schema
    .fields
    .exists(field => field.name == targetCol)

if (colExists(df, "ID_Individuals")) { 
.. //rename otherwise print warn 
}

When the renaming actually happens show() will no longer print your old column.

  • Related