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

If there's a pandas row with two distinct value, do this

I have a pandas dataframe (df) that looks like this

   Phase  Sig  EJO
0      1    -1    2
1      1     0    2
2      1     1    2
3      2    -1    7
4      2     1    1

I want to figure how to write a statement such that if there is any row with a 1 for Phase and 0 for Sig to multiple that row’s EJO by 2

This is my desired output

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

   Phase  Sig  EJO
0      1    -1    2
1      1     0    4
2      1     1    2
3      2    -1    7
4      2     1    1

I will be doing this on a much larger scale

>Solution :

You can use np.where:

df['EJO'] = df['EJO'] * np.where(df['Phase'].eq(1) & df['Sig'].eq(0), 2, 1)

Output:

>>> df
   Phase  Sig  EJO
0      1   -1    2
1      1    0    4
2      1    1    2
3      2   -1    7
4      2    1    1
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