pandas changed column value condition of three other columns

I have the following pandas dataframe:

df = pd.DataFrame({'pred': [1, 2, 3, 4],
                   'a': [0.4, 0.6, 0.35, 0.5],
                   'b': [0.2, 0.4, 0.32, 0.1],
                   'c': [0.1, 0, 0.2, 0.2],
                   'd': [0.3, 0, 0.1, 0.2]})

I want to change values on ‘pred’ column, based on columns a,b,c,d , as following:

if a has the value at column a is larger than the values of column b,c,d
and
if one of columns – b , c or d has value larger than 0.25

then change value in ‘pred’ to 0. so the results should be:

    pred    a       b         c      d
0   1        0.4    0.2      0.1    0.1
1   0        0.6    0.4      0.0    0.0
2   0        0.35   0.32      0.2   0.3
3   4        0.5    0.1      0.2    0.2

How can I do this?

>Solution :

Create a boolean condition/mask then use loc to set value to 0 where condition is True

cols = ['b', 'c', 'd']
mask = df[cols].lt(df['a'], axis=0).all(1) & df[cols].gt(.25).any(1)
df.loc[mask, 'pred'] = 0

   pred     a     b    c    d
0     1  0.40  0.20  0.1  0.1
1     0  0.60  0.40  0.0  0.0
2     0  0.35  0.32  0.2  0.3
3     4  0.50  0.10  0.2  0.2

Leave a Reply