How to drop rows of Pandas DataFrame whose value in a certain column is NaN

前端 未结 12 856
一生所求
一生所求 2020-11-22 00:59

I have this DataFrame and want only the records whose EPS column is not NaN:

>>> df
                 STK_ID           


        
12条回答
  •  失恋的感觉
    2020-11-22 01:22

    In datasets having large number of columns its even better to see how many columns contain null values and how many don't.

    print("No. of columns containing null values")
    print(len(df.columns[df.isna().any()]))
    
    print("No. of columns not containing null values")
    print(len(df.columns[df.notna().all()]))
    
    print("Total no. of columns in the dataframe")
    print(len(df.columns))
    

    For example in my dataframe it contained 82 columns, of which 19 contained at least one null value.

    Further you can also automatically remove cols and rows depending on which has more null values
    Here is the code which does this intelligently:

    df = df.drop(df.columns[df.isna().sum()>len(df.columns)],axis = 1)
    df = df.dropna(axis = 0).reset_index(drop=True)
    

    Note: Above code removes all of your null values. If you want null values, process them before.

提交回复
热议问题