Home > Back-end >  If there's a pandas row with two distinct value, do this
If there's a pandas row with two distinct value, do this

Time:02-10

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

   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

CodePudding user response:

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
  • Related