How to find out if a field is not empty('') and not null in a pandas dataframe?
I am using if not value and pd.notna(value):
Is it correct or there is a better API which handles this logic directly?
You could replace fields with empty strings or containing only blank spaces with np.nan or None in your dataframe and then use notna to find valid values.
df.replace(r'^\s*$', np.nan, regex=True, inplace=True)
Moreover, if not value and pd.notna(value) does not work for an empty string or a field containing only blank spaces:
value = ''
not value and pd.notna(value) # this is True
value = None
not value and pd.notna(value) # this is False
value = np.nan
not value and pd.notna(value) # this is False
How about changing empty value to NaN and then check. Kr.
df = pd.DataFrame({"a": [np.nan, 1, ""]})
df = df.replace('', np.nan)
print(df.isnull())
which returns:
a
0 True
1 False
2 True
Alternative is to fill NaN by empty '' then check:
df = pd.DataFrame({"a": [np.nan, 1, ""]})
df = df.fillna('')
print(df=='')
which returns:
a
0 True
1 False
2 True