Home » How to Fix in Pandas: The truth value of a Series is ambiguous

How to Fix in Pandas: The truth value of a Series is ambiguous

by Erma Khan

One error you may encounter in Python is:

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

This error usually occurs when you attempt to filter a pandas DataFrame using the words and and or instead of using the & and | operators.

This tutorial shares how to resolve this error in practice.

How to Reproduce the Error

Suppose we create the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'points': [18, 22, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A      18        5        11
1    A      22        7         8
2    A      19        7        10
3    A      14        9         6
4    B      14       12         6
5    B      11        9         5
6    B      20        9         9
7    B      28        4        12

Now suppose we attempt to filter for rows where the team is equal to “A” and the points is less than 20:

#attempt to filter DataFrame
df[(df['team'] == 'A') and (df['points'] 20)]

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

Or suppose we attempt to filter for rows where the team is equal to “A” or the points is less than 20:

#attempt to filter DataFrame
df[(df['team'] == 'A') or (df['points'] 20)]

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

In both scenarios we receive an error that tells us the truth value of a Series is ambiguous.

How to Fix the Error

To avoid this error when filtering, we need to make sure we use the & and | operators.

For example, we can use the following code to filter for rows where the team is equal to “A” and the points is less than 20:

#filter DataFrame
df[(df['team'] == 'A') & (df['points'] 20)]

        team	points	assists	rebounds
0	A	18	5	11
2	A	19	7	10
3	A	14	9	6

Or we could use the following code to filter for rows where the team is equal to “A” or the points is less than 20:

#filter DataFrame
df[(df['team'] == 'A') | (df['points'] 20)]

        team	points	assists	rebounds
0	A	18	5	11
1	A	22	7	8
2	A	19	7	10
3	A	14	9	6
4	B	14	12	6
5	B	11	9	5

In both scenarios we don’t receive an error since we used the & and | operators.

Note: It’s important that you include parenthesis around each individual condition when filtering a pandas DataFrame by multiple conditions, otherwise you will receive an error.

Additional Resources

The following tutorials explain how to fix other common errors in Python:

How to Fix: module ‘pandas’ has no attribute ‘dataframe’
How to Fix in Pandas: SettingWithCopyWarning
How to Fix in Pandas: TypeError: no numeric data to plot

Related Posts