-3

Hello I have this code and in my dataframe I have some NaN values and I'd like to use the for a condition as you can see



if df['stoch_slowk'].isna():
    df['action']='attendre'
if df['stoch_slowk'] < 20:
    df['action'] ='Vendre'
elif df['stoch_slowk'] > 80:
    df['action'] ='Acheter'
elif df['stoch_slowk'] > 20 and df['stoch_slowk'] < 80:
    if df['stoch_slowd'] > 20:
        df['action'] ='Acheter'
    elif df['stoch_slowd'] < 80:
        df['action'] ='Vendre'
else:
    df['action'] ='Attendre'

If you could help me, thank you

I have this error : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

1 Answers1

1

df['stoch_slowk'].isna() returns a Series with True or False for each row. You can use any() or all() to reduce the Series to a single True or False value:

df['stoch_slowk'].isna().any() # True if any of the values in 'stoch_slowk' is NaN

If you want to change the value of rows where with a NaN value, you can use the following:

na_mask = df['stoch_slowk'].isna()
df['action'][na_mask] = 'attendre'

dkruit
  • 135
  • 6