Retrieve indices of NaN values in a pandas dataframe

前端 未结 4 1661
粉色の甜心
粉色の甜心 2020-12-18 03:50

I try to retrieve for each row containing NaN values all the indices of the corresponding columns.

d=[[11.4,1.3,2.0, NaN],[11.4,1.3,NaN, NaN],[11.4,1.3,2.8,          


        
4条回答
  •  鱼传尺愫
    2020-12-18 04:02

    It should be efficient to use a scipy coordinate-format sparse matrix to retrieve the coordinates of the null values:

    import scipy.sparse as sp
    
    x,y = sp.coo_matrix(df.isnull()).nonzero()
    print(list(zip(x,y)))
    
    [(0, 3), (1, 2), (1, 3), (3, 0), (3, 1)]
    

    Note that I'm calling the nonzero method in order to just output the coordinates of the nonzero entries in the underlying sparse matrix since I don't care about the actual values which are all True.

提交回复
热议问题