Search code examples
pythonpandasvalueerrorpandas-loc

ValueError: The truth value of a Series is ambiguous while using lambda with loc


Df.loc[lambda Df: Df['score'] > 15 and Df['score'] < 20]

I am getting the mentioned error while using the above-mentioned code. Thanks in advance Error : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().


Solution

  • The main issue is the lack of parenthesis around the expressions.

    There are two other issues in your code:

    • Use '&' instead of 'and' when working with Series.
    • No need to use lambda.

    Here's a piece of code that would work:

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