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列的dataframe。我想知道是否有任何行包含空值,并将这些'null'行放入一个单独的dataframe中,以便我可以轻松地研究它们。
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)?
是否有一种更优雅的方法(查找包含null的行)?
2 个解决方案
#1
196
[Updated to adapt to modern pandas
, which has isnull
as a method of DataFrame
s..]
[更新以适应现代熊猫,它有isnull作为数据加密的方法。]
You can use isnull
and any
to build a boolean Series and use that to index into your frame:
您可以使用isnull和any构建一个布尔序列,并使用它将其索引到您的框架中:
>>> 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:
你可以用函数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
#2
15
nans = lambda df: df[df.isnull().any(axis=1)]
then when ever you need it you can type:
当你需要的时候,你可以输入:
nans(your_dataframe)
#1
196
[Updated to adapt to modern pandas
, which has isnull
as a method of DataFrame
s..]
[更新以适应现代熊猫,它有isnull作为数据加密的方法。]
You can use isnull
and any
to build a boolean Series and use that to index into your frame:
您可以使用isnull和any构建一个布尔序列,并使用它将其索引到您的框架中:
>>> 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:
你可以用函数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
#2
15
nans = lambda df: df[df.isnull().any(axis=1)]
then when ever you need it you can type:
当你需要的时候,你可以输入:
nans(your_dataframe)