i have a simple dataframe like:
df
id_ente_competenza_tipo | INOLTRO_PSAP2
3 S
3 S
3 N
2 S
i have another dataframe static table:
df_ente
id_ente_competenza_tipo| des_ente_competenza_tipo
1 Carabinieri
2 Polizia di Stato
3 Emergenza Sanitaria
i want that if df.ID_ENTE_COMPETENZA_TIPO =='S' --> df.ID_ENTE_COMPETENZA_TIPO =df_ente.des_ente_competenza_tipo where df.id_ente_competenza_tipo ==df_ente.id_ente_competenza_tipo otherwise df.id_ente_competenza_tipo
i want this:
id_ente_competenza_tipo | INOLTRO_PSAP2
Emergenza Sanitaria S
Emergenza Sanitaria S
3 N
Polizia di Stato S
my code is this but there is amoreefficient way?
df=df.withColumn("ID_ENTE_COMPETENZA_TIPO",F.when(df.INOLTRO_PSAP2=="S",df_ente.join(df,
df_ente["id_ente_competenza_tipo"]==df["ID_ENTE_COMPETENZA_TIPO"])\
.select("des_ente_competenza_tipo").head()[0]).otherwise(df.ID_ENTE_COMPETENZA_TIPO))
Thanks for help Regards