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]
.