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

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

我将这段代码用于 if 语句:

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

两种类型的 if 语句都不起作用并抛出错误:

ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

之前针对此错误给出的解决方案是 np.where 和从数据框中选择选择性值,但我的解决方案是关于 if 语句。

请帮忙。

谢谢

有几个奇怪的代码片段:

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

由于缺少 df2、np 和 stats 的样本值而未测试