In pyspark, I have a dataframe with 3 columns and I want to replace part of a column value with a value from a dictionary. df.replace will replace only if the entire column value is part of the dictionary, but I want to replace the value from a part of the column value. For instance:
id = [("val1","01-JAN-2021","01-MAR-2021"),
("val2","02-JUL-2021","01-AUG-2022"),
("val3","02-JUL-2021",None)]
df = spark.createDataFrame(id,["id","start_date","end_date"])
df = df.replace(date_dict, subset = ["start_date", "end_date"])
Dictionary is:
date_dict = {"JAN": "Jan",
"FEB" : "Feb",
"MAR" : "Mar",
"APR" : "Apr",
"MAY" : "May",
"JUN" : "Jun",
"JUL" : "Jul",
"AUG" : "Aug",
"SEP" : "Sep",
"OCT" : "Oct",
"NOV" : "Nov",
"DEC" : "Dec"}
I tried the below:
df = df.replace(date_dict, subset = ["start_date", "end_date"])
but it would not replace as the column values are say '01-JUN-2020' vs just "JUN". Any idea how to achieve this?