Find empty or NaN entry in Pandas Dataframe

前端 未结 8 2450
不思量自难忘°
不思量自难忘° 2020-12-05 04:17

I am trying to search through a Pandas Dataframe to find where it has a missing entry or a NaN entry.

Here is a dataframe that I am working with:

cl_         


        
8条回答
  •  离开以前
    2020-12-05 05:07

    Check if the columns contain Nan using .isnull() and check for empty strings using .eq(''), then join the two together using the bitwise OR operator |.

    Sum along axis 0 to find columns with missing data, then sum along axis 1 to the index locations for rows with missing data.

    missing_cols, missing_rows = (
        (df2.isnull().sum(x) | df2.eq('').sum(x))
        .loc[lambda x: x.gt(0)].index
        for x in (0, 1)
    )
    
    >>> df2.loc[missing_rows, missing_cols]
             A2       A3
    2            1.10035
    5 -0.508501         
    6       NaN      NaN
    7       NaN      NaN
    

提交回复
热议问题