So I tried a bunch of stuff but nothing seems to work. See the dataframe.
df
| index | Bool | New_Bool |
|---|---|---|
| 1 | True | True |
| 2 | True | True |
| 3 | True | True |
| 4 | False | True |
I want to update a column (New_bool). It should contain False if column Bool is false already AND it also should contain false in row with index=1 if Bool in row with index=4 is false as well. All other rows should be kept as are. I cannot manage to do this...see expected output down below.
| index | Bool | New_Bool |
|---|---|---|
| 1 | True | False |
| 2 | True | True |
| 3 | False | False |
| 4 | False | False |
I tried different combinations of .when statements but that did not work. Best solution so far:
df = df.withColumn('New_Bool', F.when((F.col('index')==4) &
((F.col('Bool')==False), False))
But this produces:
| index | Bool | New_Bool |
|---|---|---|
| 1 | True | None |
| 2 | True | None |
| 3 | True | None |
| 4 | False | False |
If I were able to now fill all Nones with the value from row 4 in New_Bool guess that would help. I'd then set up another final column with:
df = df.withColumn('Final_Bool', F.when((F.col('index')==1) &
((F.col('New_Bool')==False), False).otherwise(F.col('Bool'))
Not sure about anything anymore though because I tried so many combinations....by the way I will have many groups of those dataframes that's why I need a solution that works with groupby or a window function.
Happy for every advice :)