在Pandas DataFrame中查找非NaN值的索引 [英] Find Indexes of Non-NaN Values in Pandas DataFrame

查看:863
本文介绍了在Pandas DataFrame中查找非NaN值的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的数据集(大约200000x400),但是我对其进行了过滤,仅剩下几百个值,其余为NaN.我想创建这些剩余值的索引列表.我似乎找不到足够简单的解决方案.

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 of those remaining values. I can't seem to find a simple enough solution.

    0     1     2
0   NaN   NaN   1.2
1   NaN   NaN   NaN   
2   NaN   1.1   NaN   
3   NaN   NaN   NaN
4   1.4   NaN   1.01

例如,我想要一个[[0,2),(2,1),(4,0),(4,2)]的列表.

For instance, I would like a list of [(0,2), (2,1), (4,0), (4,2)].

推荐答案

将数据框转换为等效的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)]

这篇关于在Pandas DataFrame中查找非NaN值的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆