Python Pandas find all rows where all values are NaN

后端 未结 3 804
囚心锁ツ
囚心锁ツ 2020-12-06 01:16

So I have a dataframe with 5 columns. I would like to pull the indices where all of the columns are NaN. I was using this code:

nan = pd.isnull(df.all)
         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-06 01:32

    It should just be:

    df.isnull().all(1)
    

    The index can be accessed like:

    df.index[df.isnull().all(1)]
    

    Demonstration

    np.random.seed([3,1415])
    df = pd.DataFrame(np.random.choice((1, np.nan), (10, 2)))
    df
    

    idx = df.index[df.isnull().all(1)]
    nans = df.ix[idx]
    nans
    


    Timing

    code

    np.random.seed([3,1415])
    df = pd.DataFrame(np.random.choice((1, np.nan), (10000, 5)))
    

提交回复
热议问题