Pyspark set values based on column's condition

Viewed 30

I have this dataframe

data = [['tom','kik',1], ['nick','ken', 1], ['juli','ryan', 2]]
  
df = pd.DataFrame(data, columns=['Name','Name2', 'stat'])
  
df= spark.createDataFrame(df)

I need to make this transformation for the two columns (if stat ==1 then Name and Name2 ==Toto)

data = [['toto','toto', 1], ['toto','toto',1], ['juli','juli', 2]]
  
df = pd.DataFrame(data, columns=['Name','Name2', 'stat'])
  
df= spark.createDataFrame(df)
1 Answers
from pyspark.sql.functions import col, when

condition = (col("stat")==1)
new_df = df.withColumn("Name", when(condition, "toto")).withColumn("Name2", when(condition, "toto"))
Related