Python Pandas查找所有值均为NaN的所有行 [英] Python Pandas find all rows where all values are NaN
本文介绍了Python Pandas查找所有值均为NaN的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个包含5列的数据框.我想提取所有列均为NaN的索引.我正在使用此代码:
So I have a dataframe with 5 columns. I would like to pull the indices where all of the columns are NaN. I was using this code:
nan = pd.isnull(df.all)
但这只是返回false,因为从逻辑上讲并非并非数据框中的所有值都为null.有成千上万的条目,所以我宁愿不必遍历并检查每个条目.谢谢!
but that is just returning false because it is logically saying no not all values in the dataframe are null. There are thousands of entries so I would prefer to not have to loop through and check each entry. Thanks!
推荐答案
应为:
df.isnull().all(1)
index
的访问方式如下:
df.index[df.isnull().all(1)]
演示
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, np.nan), (10, 2)))
df
idx = df.index[df.isnull().all(1)]
nans = df.ix[idx]
nans
代码
np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, np.nan), (10000, 5)))
这篇关于Python Pandas查找所有值均为NaN的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文