Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading