0

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().

Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
ThatQuantDude
  • 759
  • 1
  • 9
  • 26
  • Your `Signal` column is dected as a boolean by pandas. You want to change only some values, so the whole column type will no change. Try forcing the column to be str type. – Cyrille Pontvieux Mar 16 '21 at 14:14
  • Does `DataFrame.where` solve your problem? – runDOSrun Mar 16 '21 at 14:17
  • @CyrillePontvieux I don't think that's the problem. `df_in['COND1'].shift()` returns a column, as does `df_in['COND2'].shift()`. The result of the boolean expression is a column, so you can't run an `if` on it. OP, [this](https://stackoverflow.com/questions/10715519/conditionally-fill-column-values-based-on-another-columns-value-in-pandas) should answer your question though. – Pranav Hosangadi Mar 16 '21 at 14:19

0 Answers0