【问题标题】:Dealing with ValueError: The truth value of a Series is ambiguous | for multiple conditions in finding indices of pandas处理ValueError:Series的真值不明确|寻找熊猫指数的多种条件
【发布时间】:2021-06-02 14:27:33
【问题描述】:

当我想达到一个条件的行索引时,我会这样做:

df[df['label'] <= x].index

output: 
Int64Index([   44,    45,    46,    47,    48,    49,    50,    51,    52,
               53,
            ...
            28603, 28604, 28613, 28622, 28623, 28624, 28632, 28633, 28646,
            28647],
           dtype='int64', length=3710)

但如果我这样做:

df[df['label'] >= y & df['label'] <= x].index

我收到值错误:

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

任何提示和技巧将不胜感激!

【问题讨论】:

  • 每个条件都需要加上括号df[(df['label'] &gt;= y) &amp; (df['label'] &lt;= x)]

标签: python pandas indexing conditional-statements


【解决方案1】:

尝试添加 parethsis 或只做 gele

df[df['label'].ge(y) & df['label'].le(x)].index

【讨论】:

    猜你喜欢
    • 2020-08-03
    • 2021-04-07
    • 2021-09-10
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2020-03-30
    • 2018-06-14
    • 2016-07-11
    相关资源
    最近更新 更多