【问题标题】:If statement, ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()If 语句,ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
【发布时间】:2020-02-12 17:53:40
【问题描述】:

我将这段代码用于 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 语句。

请帮忙。

谢谢

【问题讨论】:

  • 你能给出 df2、np 和 stats 的样本数据吗?

标签: python-3.x if-statement valueerror


【解决方案1】:

有一些奇怪的代码片段:

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 的样本值而未经测试

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 2019-01-24
    • 2020-05-27
    • 1970-01-01
    • 2021-03-21
    • 2021-05-27
    • 2021-04-07
    • 2022-12-29
    • 2018-06-09
    相关资源
    最近更新 更多