1

How to alter the column datatype based on the input parameter using pyspark

from pyspark.sql.types import IntegerType,BooleanType,DateType
from pyspark.sql.functions import col
Column_Name="EFFECTIVE_DATE"
df=spark.sql(f"select * from TableA")
df.withColumn({Column_Name},col({Column_Name}).cast(DateType())
Venkatesh
  • 91
  • 1
  • 9

1 Answers1

0

input come always through widget:

input = dbutils.widgets.get("X")

so assume that:

input == "int"

then instead of using IntegerType() function you can just use string "int" type as cast so:

df.withColumn({Column_Name},col({Column_Name}).cast(input)
Hubert Dudek
  • 1,666
  • 1
  • 13
  • 21