jeudi 30 juin 2016

Dataframe conditionl logic

I have a dataframe ('dayData') with the columns 'Power1' and 'Power2'

      Power1         Power2   
 1.049246442   -0.231991505  
-0.950753558    0.276990531  
-0.950753558    0.531481549  
 0             -0.231991505  
-0.464648091   -0.231991505  
 1.049246442   -1.204952258   
 0.455388896   -0.486482523   
 0.879383766    0.226092327   
-0.50417844     0.83687077   
 0.152025349   -0.359237014  

I'm trying to use conditional logic to create the 'resultPower' column. For each row, the logic I'm trying to install is: if (Power1 >= 0 AND Power2 =<0) OR if (Power1 <= 0 AND Power2 >= 0) then 0,return the value for Power1.

So when the resultPower column is added the dataframe would look like:

      Power1         Power2   ResultPower
 1.049246442   -0.231991505             0
-0.950753558    0.276990531             0
-0.950753558    0.531481549             0
 0             -0.231991505             0
-0.464648091   -0.231991505  -0.464648091
 1.049246442   -1.204952258             0
 0.455388896   -0.486482523             0
 0.879383766    0.226092327   0.879383766
-0.50417844     0.83687077              0
 0.152025349   -0.359237014             0

I have used basic conditional logic in pandas before, for example I would be able to check one of the logic conditions i.e.

dayData['ResultPower'] = np.where(dayData.Power1 > 0, 0, dayData.Power1)

but I can't find how I can add logic conditions with AND / OR functions. To build something like

dayData['ResultPower'] = np.where(dayData.Power1 >= 0 and dayData.Power2 =< 0 or dayData.Power1 =< 0 and dayData.Power2 >= 0, 0, dayData.Power1)

Could someone let me know if this is possible and the syntax for doing this please?

Thanks

Aucun commentaire:

Enregistrer un commentaire