Home > Software engineering >  Python: Check if a variable is NA in the if statement
Python: Check if a variable is NA in the if statement

Time:05-05

I have a function that looks like this:

if (Var_A   Var_B <7):
    Do something ...

The issue is, the Var_B column in the dataframe has either integers, or NaN values.

If I write the function as it is now, it is returning error as "TypeError: boolean value of NA is ambiguous".

How should I re-write the function so that, if Var_B is NaN, just skip the comparison and return Var_A?

Is it something like:

if Var_B is not null:
 if (Var_A   Var_B <7):
    Do something ...
 else:
    Var_A 

Or, I can do something more elegant without adding an if statement:

if (Var_A   Var_B < 7) and (Var_B is not null):
   Do something ... 

Additionally, how exactly do I write the syntax that checks Var_B is not null? I tried to write != isnull() and !is.na() but both are not working..

Very new to Python, much appreciation for your help!

CodePudding user response:

IIUC use if working with scalars:

Var_A = 1
Var_B = 2

if (Var_A   Var_B < 7) and pd.notna(Var_B):
    print ('pass')
    
pass
    
Var_A = 1
Var_B = np.nan

if (Var_A   Var_B < 7) and pd.notna(Var_B):
    print ('pass')

If working with columns ouput is boolean mask, code is changed:

df = pd.DataFrame({'Var_A':[1,2,3], 'Var_B':[np.nan, 2, 7]})

print (df)
0    False
1     True
2    False
dtype: bool

mask = ((df.Var_A   df.Var_B) < 7) & df.Var_B.notna()
print (mask)
0    False
1     True
2    False
dtype: bool

But then if mask: cannot be used, because using array.

More infotmation in link

CodePudding user response:

Pandas has the isnull function that returns True or False if the column's item is Nan or no Nan. So try doing it this way

if not Var_B.isnull().values.any():
    if (Var_A   Var_B <7):
        Do something ...
else:
    Var_A 

CodePudding user response:

simply do

df = df.fillna(0) 
# if you want to get rid of all NaN values and replace with 0

(or)

df['DataFrame Column'] = df['DataFrame Column'].fillna(0) 
# if you only want to replace NaN in a particular column to 0

Now you can proceed with adding/subtracting/ whatever you want to do.

CodePudding user response:

You could also try something like this, it usually works for me:

def func(a,b):
  
  try:
    check = np.isnan(b) 
    if check == True:
      print("B is nan")
      print(int(a))
    else:
      np.sum(a b)
      if a b<7:
          print("Something")
  except TypeError:
    print("Error")
  

for x in .....:
  for y in .....:
    func(x,y)
  • Related