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

Time:11-18

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.

CodePudding user response:

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
  • Related