mardi 16 mars 2021

Python list comprehension if else statement and shift

Let's assume I have a df like this

Signal.   COND1.    COND2
  NO        1.        1
  NO        1         3
  NO        4         2

I would like to be able to have a list comprehension that overwrites the Signal column by using the following condition

Write "GO" if COND1_t-1 < COND2_t-1 and COND1_t0 > COND2_to 

I tried sth like this

 df_in['SIGNAL'] = ['GO' for n in df_in['SIGNAL'] if ((df_in['COND1'].shift(1) < df_in['COND2'].shift(1)) & (df_in['COND1'] > df_in['COND2']))]

But I am getting ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Aucun commentaire:

Enregistrer un commentaire