【发布时间】: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'] >= y) & (df['label'] <= x)]
标签: python pandas indexing conditional-statements