samedi 16 novembre 2019

If, elif, else statement error: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() [duplicate]

I have a following table:

YearMonth   language     Rate        SMA_3     SMA_3_dev

2019-11-01    de        0.018       0.0259      0.0022

And I tried to reset SMA_3_dev value according to the condition:

def test(df):

    if df['SMA_3_dev'] < 0.01:
        df['SMA_3_dev'] = df['SMA_3'] + df['SMA_3_dev']

    elif df['SMA_3_dev'] < 0.02:
        df['SMA_3_dev'] = df['SMA_3'] + df['SMA_3_dev']/2

    elif df['SMA_3_dev'] < 0.03:
        df['SMA_3_dev'] = df['SMA_3'] + df['SMA_3_dev']/3

    else:
        df['SMA_3_dev'] = df['SMA_3'] + df['SMA_3_dev']/4

    return df

test(df1)

However, I got the following error

> --------------------------------------------------------------------------- ValueError                                Traceback (most recent call
> last) <ipython-input-244-41c86144f8d5> in <module>
> ----> 1 test(df1)
> 
> <ipython-input-243-023c1d1776eb> in test(df)
>       1 def test(df):
>       2 
> ----> 3     if df['SMA_3_dev'] < 0.01:
>       4         df['SMA_3_dev'] = df['SMA_3'] + df['SMA_3_dev']
>       5 
> 
> /opt/conda/lib/python3.6/site-packages/pandas/core/generic.py in
> __nonzero__(self)    1574         raise ValueError("The truth value of a {0} is ambiguous. "    1575                          "Use a.empty,
> a.bool(), a.item(), a.any() or a.all()."
> -> 1576                          .format(self.__class__.__name__))    1577     1578     __bool__ = __nonzero__
> 

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

I don't know why is ambiguous even though there is only one row in my table? Does someone know how I can fix this issue? thank you!

Aucun commentaire:

Enregistrer un commentaire