Python Pandas 查找所有值为 NaN 的所有行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38884538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:47:20  来源:igfitidea点击:

Python Pandas find all rows where all values are NaN

pythonpython-2.7pandas

提问by Steven Setteducati Jr.

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:

所以我有一个包含 5 列的数据框。我想提取所有列都是 NaN 的索引。我正在使用此代码:

nan = pd.isnull(df.all)

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!

但这只是返回 false,因为从逻辑上讲,并非数据帧中的所有值都为空。有数千个条目,所以我宁愿不必循环检查每个条目。谢谢!

采纳答案by piRSquared

It should just be:

它应该只是:

df.isnull().all(1)

The indexcan be accessed like:

index可以访问,如:

df.index[df.isnull().all(1)]

Demonstration

示范

np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, np.nan), (10, 2)))
df

enter image description here

在此处输入图片说明

idx = df.index[df.isnull().all(1)]
nans = df.ix[idx]
nans

enter image description here

在此处输入图片说明



Timing

定时

code

代码

np.random.seed([3,1415])
df = pd.DataFrame(np.random.choice((1, np.nan), (10000, 5)))

enter image description here

在此处输入图片说明

回答by Alexander

Assuming your dataframe is named df, you can use boolean indexing to check if all columns (axis=1) are null. Then take the index of the result.

假设您的数据框名为df,您可以使用布尔索引来检查所有列 ( axis=1) 是否为空。然后取结果的索引。

np.random.seed(0)
df = pd.DataFrame(np.random.randn(5, 3))
df.iloc[-2:, :] = np.nan
>>> df
          0         1         2
0  1.764052  0.400157  0.978738
1  2.240893  1.867558 -0.977278
2  0.950088 -0.151357 -0.103219
3       NaN       NaN       NaN
4       NaN       NaN       NaN

nan = df[df.isnull().all(axis=1)].index

>>> nan
Int64Index([3, 4], dtype='int64')

回答by user357269

From the master himself: https://stackoverflow.com/a/14033137/6664393

来自大师本人:https: //stackoverflow.com/a/14033137/6664393

nans = pd.isnull(df).all(1).nonzero()[0]