Python 如何在不明确列出列的情况下从 Pandas DataFrame 中选择具有一个或多个空值的行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14247586/
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-08-18 10:52:09  来源:igfitidea点击:

How to select rows with one or more nulls from a pandas DataFrame without listing columns explicitly?

pythonpandasnullnan

提问by Lev Selector

I have a dataframe with ~300K rows and ~40 columns. I want to find out if any rows contain null values - and put these 'null'-rows into a separate dataframe so that I could explore them easily.

我有一个约 300K 行和约 40 列的数据框。我想找出是否有任何行包含空值 - 并将这些“空”行放入单独的数据框中,以便我可以轻松地探索它们。

I can create a mask explicitly:

我可以明确地创建一个掩码:

mask = False
for col in df.columns: 
    mask = mask | df[col].isnull()
dfnulls = df[mask]

Or I can do something like:

或者我可以这样做:

df.ix[df.index[(df.T == np.nan).sum() > 1]]

Is there a more elegant way of doing it (locating rows with nulls in them)?

有没有更优雅的方法(定位包含空值的行)?

回答by DSM

[Updated to adapt to modern pandas, which has isnullas a method of DataFrames..]

[更新以适应现代pandas,它具有isnull作为DataFrames..的方法]

You can use isnulland anyto build a boolean Series and use that to index into your frame:

您可以使用isnullany构建一个布尔系列并使用它来索引您的框架:

>>> df = pd.DataFrame([range(3), [0, np.NaN, 0], [0, 0, np.NaN], range(3), range(3)])
>>> df.isnull()
       0      1      2
0  False  False  False
1  False   True  False
2  False  False   True
3  False  False  False
4  False  False  False
>>> df.isnull().any(axis=1)
0    False
1     True
2     True
3    False
4    False
dtype: bool
>>> df[df.isnull().any(axis=1)]
   0   1   2
1  0 NaN   0
2  0   0 NaN


[For older pandas:]

[对于老年人pandas:]

You could use the function isnullinstead of the method:

您可以使用函数isnull而不是方法:

In [56]: df = pd.DataFrame([range(3), [0, np.NaN, 0], [0, 0, np.NaN], range(3), range(3)])

In [57]: df
Out[57]: 
   0   1   2
0  0   1   2
1  0 NaN   0
2  0   0 NaN
3  0   1   2
4  0   1   2

In [58]: pd.isnull(df)
Out[58]: 
       0      1      2
0  False  False  False
1  False   True  False
2  False  False   True
3  False  False  False
4  False  False  False

In [59]: pd.isnull(df).any(axis=1)
Out[59]: 
0    False
1     True
2     True
3    False
4    False

leading to the rather compact:

导致相当紧凑:

In [60]: df[pd.isnull(df).any(axis=1)]
Out[60]: 
   0   1   2
1  0 NaN   0
2  0   0 NaN

回答by Roko Mijic

def nans(df): return df[df.isnull().any(axis=1)]

then when ever you need it you can type:

然后当你需要它时,你可以输入:

nans(your_dataframe)