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' ])
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' ])
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 ]
})
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 ]
})
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!