Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: How to properly check for multi-valued lists/arrays in DF for NaN?

Consider this example:

df = pd.DataFrame({'colA': [np.nan, [123, 124], [123, np.nan, 444]]})

for v in df.colA.values:
    if pd.isnull(v):
        print(v)
# The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How can I proceed to traverse down the elements in this column, checking for nulls first?

Adding in .any() anywhere in the area will not work nicely.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use apply and a custom function:

>>> df['colA'].apply(lambda x: np.sum(pd.isna(x)) > 0)
0     True
1    False
2     True
Name: colA, dtype: bool
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading