1

I am using pandas 'apply' function like this:

df['Geog'] = df.apply (lambda row: flagCntry(row,'country'),axis=1)

def flagCntry(row,colName):
 if (row[colName] =='US' or row[colName] =='Canada'):
 return 'North America'
elif (row[colName] ==null):# **DOES NOT work!!**
 return 'Other'

How do I perform a null check within the function, because the syntax does not work

Victor
  • 16,609
  • 71
  • 229
  • 409
  • I mean a blank string. I wish to keep my 'apply' syntax because it is existing code, I just need to add a null check. I don't want to completely rewrite it using some of the other syntax – Victor Aug 17 '18 at 16:07

3 Answers3

2

You might want to consider using pandas built in functions to perform your check.

df['Geog'] = np.nan
df.loc[df.country.isin(['US','Canada']),'Geog'] = 'North America'
df.loc[df.country.isnull(),'Geog'] = 'Other'

Otherwise you can also map a dictionnary:

my_dict = {np.nan:'Other','US':'North America','Canada':'North America'}
df['Geog'] = df.country.map(my_dict)

EDIT:

If you want to use the apply syntax, you can still use the dictionnary:

df['Geog'] = df.country.apply(lambda x : my_dict[x])

And if you want to use your custom function, one way to check if an element is null is to check whether it's different from itself:

def flagCntry(row,colName):
    if row[colName] =='US' or row[colName] =='Canada':
        return 'North America'
    elif (row[colName] != row[colName]):
        return 'Other'

df['Geog'] = df.apply(lambda row: flagCntry(row,'country'),axis=1)

And if you want to match None values but not np.nan you can use row[colName] == None instead of row[colName] != row[colName].

ysearka
  • 3,805
  • 5
  • 20
  • 41
1

Change you (row[colName] ==null) to

np.isnan(row[colName])
BENY
  • 317,841
  • 20
  • 164
  • 234
0

uh... if I understand correctly, null is C/Java syntax. You might be looking for None.

In pandas more generally, this answer should work out for you.

cyinwei
  • 11
  • 3