How to select rows with one or more nulls from a pandas DataFrame without listing columns explicitly?(如何在不明确列出列的情况下从 Pandas DataFrame 中选择具有一个或多个空值的行?)

本文介绍了如何在不明确列出列的情况下从 Pandas DataFrame 中选择具有一个或多个空值的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

mask = False对于 df.columns 中的 col:面具 = 面具 |df[col].isnull()dfnulls = df[掩码]

或者我可以这样做:

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

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

解决方案

[已更新以适应现代pandas,其中有 isnull 作为 DataFrame 的方法s..]

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

<预><代码>>>>df = pd.DataFrame([range(3), [0, np.NaN, 0], [0, 0, np.NaN], range(3), range(3)])>>>df.isnull()0 1 20 假 假 假1 假真假2 假假真3 假假假假4 假假假假>>>df.isnull().any(axis=1)0 错误1 真2 真3 错误4 错误数据类型:布尔>>>df[df.isnull().any(axis=1)]0 1 21 0 南 02 0 0 南

<小时>

[对于较旧的pandas:]

您可以使用函数 isnull 代替方法:

In [56]: df = pd.DataFrame([range(3), [0, np.NaN, 0], [0, 0, np.NaN], range(3), range(3)])在 [57] 中:df出[57]:0 1 20 0 1 21 0 南 02 0 0 南3 0 1 24 0 1 2在 [58] 中:pd.isnull(df)出[58]:0 1 20 假 假 假1 假真假2 假假真3 假假假假4 假假假假在 [59] 中:pd.isnull(df).any(axis=1)出[59]:0 错误1 真2 真3 错误4 错误

导致相当紧凑:

在[60]中:df[pd.isnull(df).any(axis=1)]出[60]:0 1 21 0 南 02 0 0 南

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.

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)?

解决方案

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

You can use isnull and any to build a boolean Series and use that to index into your frame:

>>> 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:]

You could use the function isnull instead of the method:

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

这篇关于如何在不明确列出列的情况下从 Pandas DataFrame 中选择具有一个或多个空值的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!