Search code examples
python-3.xif-statementvalueerror

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


I am using this piece of code for a if statement:

for col in df2.columns:
    a = np.array(df2[col])
    p98 = stats.scoreatpercentile(a, 98)
    p5 = stats.scoreatpercentile(a, 5)
    maxv = df2.max(axis=0)
    minv = df2.min(axis=0)
    ratiomax = maxv/p98
    print(ratiomax)
    ratiomin = minv/p5
    print(ratiomin)

    if (ratiomax <= 1.1).bool() == True:
        maxv = maxv
    else: maxv = p98

    if ratiomin <= 0.2:
        minv = minv
    else: minv = 95

Neither type of if statement are working and throwing the error:

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

The solutions given earlier on this error are np.where and choosing selective values from dataframe but mine is about if statement.

Please help.

Thanks


Solution

  • There are a few strange code pieces:

    for col in df2.columns:
        a = np.array(df2[col])
        p98 = stats.scoreatpercentile(a, 98)
        p5 = stats.scoreatpercentile(a, 5)
        maxv = df2.max(axis=0)
        minv = df2.min(axis=0)
        ratiomax = maxv/p98
        print(ratiomax)
        ratiomin = minv/p5
        print(ratiomin)
    
        # no need for bool() conversion
        # maxv = maxv ... eles is unnecessary
        # this is the shorter version of your code:
        if not ratiomax <= 1.1:
            maxv = p98
    
        # same here
        if not ratiomin <= 0.2:
            minv = 95
    

    Untested due to missing sample values of df2, np and stats