Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will look at how to fix ValueError: The truth value of a Series is ambiguous in Pandas using Python. Here, we will use two examples to understand the problem, first, we will generate an error then we will find its solution. 

Generating Error True Value Error in Pandas using Conditional Statement

Here, we will create a series and provide some data in it. We will then print information on whether the series is empty or not.

Python




import pandas as pd
  
series = pd.Series(data=['Mumbai', 'Bangalore'
                         'Chennai', 'Delhi'])
  
# This is not allowed
if series:  
    print('Pandas series have some value')
else:
    print('Pandas series is empty')


Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-e82a6939bd7b> in <module>
      5 series = pd.Series(data = [ 'Mumbai', 'Bangalore', 'Chennai', 'Delhi'])
      6 
----> 7 if series:  # This is not allowed
      8     print('Pandas series have some value')
      9 else:

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

Solution

To check whether an object (like list, dict, etc in python) is empty (None) or not, we often use ‘if object_name‘. We have done the same in the above code to check if the Pandas series object is None or not. This will result in the error – ValueError: The truth value of a Series is ambiguous. To avoid this error, we can use the ‘empty‘ attribute of the Pandas series object as shown in the below code.

Python




import pandas as pd
  
series = pd.Series(data = [ 'Mumbai', 'Bangalore'
                           'Chennai', 'Delhi'])
  
# Use this instead
if not series.empty: 
    print('Pandas series have some value')
else:
    print('Pandas series is empty')


Output:

Pandas series have some value

Generating error True Value Error in Pandas using Logical Operator

In the second example, we will create a Pandas DataFrame and query on individual columns (or Series) with some condition.

Python3




import pandas as pd
  
data = pd.DataFrame(data={
    'Name': ['Rahul', 'John', 'Raj'
             'Amit', 'Yash'],
    'Marks': [530, 450, 515, 490, 465]
})
  
# This is not allowed
new_data = data[(data['Marks'] > 475) or (
    data['Name'] == 'Yash')] 
new_data


Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-c200854fe0f8> in <module>
      8 })
      9 
---> 10 new_data = data[(data['Marks'] > 475) or (data['Name'] == 'Yash')] # This is not allowed
     11 new_data

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

Solution

Another popular case in which we might encounter the error is using standard Python logical operators – ‘and’ and ‘or’ between conditions. Pandas consider these as ambiguous. To avoid the error, we need to replace these logical operators with bit-wise operators. The ‘and’ operator will be replaced with ‘&’ while the ‘or’ operator will be replaced with the pipe operator ‘|’. The below code modifies this condition and avoids the error.

Python




import pandas as pd
  
data = pd.DataFrame(data={
    'Name': ['Rahul', 'John', 'Raj',
             'Amit', 'Yash'],
    'Marks': [530, 450, 515, 490, 465]
})
  
# Use this instead
new_data = data[(data['Marks'] > 475) | (
    data['Name'] == 'Yash')]  
new_data


Output:

    Name    Marks
0    Rahul    530
2    Raj    515
3    Amit    490
4    Yash    465


Last Updated : 05 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads