Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign values from column 1 to column 2 when column 1 is > 0 and column 2 is <= 0

I have a df,

delta1    delta2
  0         -1
  2          0
 -1          0
  0          0

I am wondering how to assign values of delta2 to delta1 only if delta1 > 0 and delta2 <= 0; the result look like,

delta1    delta2
  0         -1
  0          0
 -1          0
  0          0 
like image 407
daiyue Avatar asked Jan 26 '23 14:01

daiyue


1 Answers

Using numpy.where:

df['delta1'] = np.where(df.delta1.gt(0) & df.delta2.le(0), df.delta2, df.delta1)

   delta1  delta2
0       0      -1
1       0       0
2      -1       0
3       0       0
like image 187
user3483203 Avatar answered Jan 30 '23 01:01

user3483203