1

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?

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.