{% extends "layout.html" %} {% block body %}

Replace df with the dataframe variable you use

from itertools import chain

from pyspark.sql.functions import coalesce,lit, col, create_map

Map only changed grouping values

map_values = {{final_part}}

mapping_expr = create_map([lit(x) for x in chain(*map_values.items())])

df=df.withColumn({{col2}}, coalesce(mapping_expr[df[{{col1}}]], df[{{col2}}]))

Map all grouing values

map_values = {{final_all}}

mapping_expr = create_map([lit(x) for x in chain(*map_values.items())])

df=df.withColumn({{col2}}, coalesce(mapping_expr[df[{{col1}}]], lit(-1)))

{% endblock %}