获取大熊猫

时间:2017-02-10 00:33:54

标签: python pandas tuples nan

如何获得具有NAN值的Pandas中的所有位置。例如,

sample = pd.DataFrame(np.zeros(shape=[5,5]))
sample.iloc[0,0] = np.nan
sample.iloc[2,3] = np.nan
sample.iloc[4,3] = np.nan

是Pandas数据框,其中诸如(0,0),(2,3)和(4,3)的位置具有nan值。因此,我想要一个像这样的元组列表

[(0,0),(2,3),(4,3)]
我能得到这个吗?

此致

1 个答案:

答案 0 :(得分:1)

在这种情况下,由于列和索引值是行号和列号,您可以先使用isnull将每个条目转换为布尔值,然后使用lambda函数过滤括号中的真值,然后转选定的索引到列表中。

sample.isnull().stack()[lambda x: x].index.tolist()
[(0, 0), (2, 3), (4, 3)]