ValueError:在将 lambda 与 loc 一起使用时,Series 的真值不明确 [重复]

Posted

技术标签:

【中文标题】ValueError:在将 lambda 与 loc 一起使用时,Series 的真值不明确 [重复]【英文标题】:ValueError: The truth value of a Series is ambiguous while using lambda with loc [duplicate] 【发布时间】:2020-10-04 01:46:40 【问题描述】:
Df.loc[lambda Df: Df['score'] > 15 and Df['score'] < 20]

我在使用上述代码时遇到了上述错误。提前致谢 Error : ValueError: Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

【问题讨论】:

***.com/questions/21415661/… 这能回答你的问题吗? Logical operators for boolean indexing in Pandas 【参考方案1】:

主要问题是表达式周围缺少括号。

您的代码中还有另外两个问题:

使用 Series 时使用“&”而不是“and”。 无需使用 lambda。

下面是一段可行的代码:

df.loc[(df['score'] > 15) & (df['score'] < 20)]

【讨论】:

以上是关于ValueError:在将 lambda 与 loc 一起使用时,Series 的真值不明确 [重复]的主要内容,如果未能解决你的问题,请参考以下文章