Find Indexes of Non-NaN Values in Pandas DataFrame

后端 未结 2 1777
我寻月下人不归
我寻月下人不归 2020-12-21 04:14

I have a very large dataset (roughly 200000x400), however I have it filtered and only a few hundred values remain, the rest are NaN. I would like to create a list of indexes

2条回答
  •  没有蜡笔的小新
    2020-12-21 04:27

    Convert the dataframe to it's equivalent NumPy array representation and check for NaNs present. Later, take the negation of it's corresponding indices (indicating non nulls) using numpy.argwhere. Since the output required must be a list of tuples, you could then make use of generator map function applying tuple as function to every iterable of the resulting array.

    >>> list(map(tuple, np.argwhere(~np.isnan(df.values))))
    [(0, 2), (2, 1), (4, 0), (4, 2)]
    

提交回复
热议问题