Hello guys I'm doing a dataframe filtering based on if condition but the problem that I must repeat the same code 3 times in every if condition and I don't want to do that. It's not optimized. Someone has any idea how to optimize that? here is the code exemple
if sexe == "male": new_df = ( df.where(F.col("sexe") == 1) .where(F.col("column_flag") == False) .withColumn("new_column", F.col("column1") / F.col("column3")) ) elif sexe == "female": new_df = ( df.where(F.col("sexe") == 2) .where(F.col("column_flag") == False) .withColumn("new_column", F.col("column1") / F.col("column3")) ) else: new_df = df.where(F.col("column_flag") == False).withColumn( "new_column", F.col("column1") / F.col("column3") )