Nan values in a Pandas DataFrame can be tricky to work with. In this post, I’ll show you how to correctly account for them when writing a function with conditionals.
To check if the value in the column is not null, you can wrap the column in pd.notnull(x['column_name'])
or pd.isnull(x['column_name'])
Here is an example function:
def some_function(x): if pd.notnull(x['column_name']): return 'this value is not null' elif pd.isnull(x['column_name']): return 'this value is null' else: pass
Further Explanation
Here’s an example of how you can run the above function on a DataFrame.
There are many ways to check if a value is NaN in Python but it’s important to wrap the cell value in the pd.notnull()
or pd.isnull()
operators to get your desired results when using Pandas. I thought you could treat the value as any variable and try the test of == None
or == pd.nan
, but these just return False
in every case.
Thanks for reading!