且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Pandas DataFrame中查找非NaN值的索引

更新时间:2021-11-02 10:02:16

将数据框转换为等效的NumPy数组表示形式,并检查是否存在NaNs.稍后,使用 numpy.argwhere .由于所需的输出必须是元组列表,因此您可以利用生成器map函数,将tuple作为函数应用于结果数组的每个可迭代对象.

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)]