jeudi 29 juillet 2021

np where with two conditions and met first

I am trying to create a target variable based on 2 conditions. I have X values that are binary and X2 values that are also binary. My condition is whenver X changes from 1 to zero, we have one in y only if it is followed by a change from 0 to 1 in X2. If that was followed by a change from 0 to 1 in X then we don't do the change in the first place. I attached a picture from excel.

I also did the following to account for the change in X

df['X-prev']=df['X'].shift(1)
df['Change-X;]=np.where(df['X-prev']+df['X']==1,1,0)

however, this is not enough as I need to know which change came first after the X change. I attached a picture of the example.

Thanks a lot for all the contributions. Example

Aucun commentaire:

Enregistrer un commentaire