I'm new to working with pandas dataframes and was trying to figure out a way to locate NaN values inside a cell of a dataframe. I'm using an if condition with isna() function.
My approach is that if the value is NaN, print "value empty" instead of returning a Boolean "True". This is my approach:
import pandas as pd
x= pd.read_csv("mdata_short.csv", header =[0])
print(x["retail_price"].iloc[12:13])
if x["retail_price"].iloc[12:13].isna() == True:
    print("value empty")
I expected a "value empty output", but I'm getting an error saying:
"ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
I tried removing "== True" part in the IF statement, still getting the same error. I did a google search but couldn't find anything related to this issue. I don't understand where I'm going wrong.
Thank you in advance for your help.
series.isna returns a series no matter you call it on whole series or a single item of series. It means your command compares a series to a single True values. Pandas doesn't want to guess how to compare, so it returns that error.
Try this:
if x["retail_price"].iloc[12:13].isna().values[0] == True:
    print('empty')
                        There is problem if select by indexing 12:13 get one element Series, so for scalar use Series.item:
if x["retail_price"].iloc[12:13].isna().item():
    print("value empty")
Another solution is convert values to numpy array and select first value:
if x["retail_price"].iloc[2:3].isna().values[0]:
    print("value empty")
Better is select by scalar value for scalar in output:
if pd.isna(x["retail_price"].iloc[12]):
    print("value empty")
Sample:
x = pd.DataFrame({
    'retail_price': [1,2,np.nan]
})
print (x)
   retail_price
0           1.0
1           2.0
2           NaN
print (x["retail_price"].iloc[2:3].isna())
2    True
Name: retail_price, dtype: bool
print (x["retail_price"].iloc[2:3].isna().item())
True
print (x["retail_price"].iloc[2:3].isna().values[0])
True
print (pd.isna(x["retail_price"].iloc[2]))
True
For comparing in boolean Series (same working for one element Series) check Using If/Truth Statements with pandas.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With