计算pandas DataFrame中缺少值的行数的最佳方法

20 python missing-data pandas

我目前想出了一些工作来计算熊猫中缺失值的数量DataFrame.那些非常难看,我想知道是否有更好的方法来做到这一点.

让我们创建一个例子DataFrame:

from numpy.random import randn
df = pd.DataFrame(randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],
               columns=['one', 'two', 'three'])
df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我现在拥有的是

a)计算缺少值的单元格:

>>> sum(df.isnull().values.ravel())
9
Run Code Online (Sandbox Code Playgroud)

b)计算某处缺少值的行:

>>> sum([True for idx,row in df.iterrows() if any(row.isnull())])
3
Run Code Online (Sandbox Code Playgroud)

EdC*_*ica 24

对于第二个计数,我认为只从从以下位置返回的行数中减去行数dropna:

In [14]:

from numpy.random import randn
df = pd.DataFrame(randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],
               columns=['one', 'two', 'three'])
df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
df
Out[14]:
        one       two     three
a -0.209453 -0.881878  3.146375
b       NaN       NaN       NaN
c  0.049383 -0.698410 -0.482013
d       NaN       NaN       NaN
e -0.140198 -1.285411  0.547451
f -0.219877  0.022055 -2.116037
g       NaN       NaN       NaN
h -0.224695 -0.025628 -0.703680
In [18]:

df.shape[0] - df.dropna().shape[0]
Out[18]:
3
Run Code Online (Sandbox Code Playgroud)

第一个可以使用内置方法实现:

In [30]:

df.isnull().values.ravel().sum()
Out[30]:
9
Run Code Online (Sandbox Code Playgroud)

计时

In [34]:

%timeit sum([True for idx,row in df.iterrows() if any(row.isnull())])
%timeit df.shape[0] - df.dropna().shape[0]
%timeit sum(map(any, df.apply(pd.isnull)))
1000 loops, best of 3: 1.55 ms per loop
1000 loops, best of 3: 1.11 ms per loop
1000 loops, best of 3: 1.82 ms per loop
In [33]:

%timeit sum(df.isnull().values.ravel())
%timeit df.isnull().values.ravel().sum()
%timeit df.isnull().sum().sum()
1000 loops, best of 3: 215 µs per loop
1000 loops, best of 3: 210 µs per loop
1000 loops, best of 3: 605 µs per loop
Run Code Online (Sandbox Code Playgroud)

所以我的替代品对于这个尺寸的df来说要快一点

更新

因此对于80,000行的df,我得到以下结果:

In [39]:

%timeit sum([True for idx,row in df.iterrows() if any(row.isnull())])
%timeit df.shape[0] - df.dropna().shape[0]
%timeit sum(map(any, df.apply(pd.isnull)))
%timeit np.count_nonzero(df.isnull())
1 loops, best of 3: 9.33 s per loop
100 loops, best of 3: 6.61 ms per loop
100 loops, best of 3: 3.84 ms per loop
1000 loops, best of 3: 395 µs per loop
In [40]:

%timeit sum(df.isnull().values.ravel())
%timeit df.isnull().values.ravel().sum()
%timeit df.isnull().sum().sum()
%timeit np.count_nonzero(df.isnull().values.ravel())
1000 loops, best of 3: 675 µs per loop
1000 loops, best of 3: 679 µs per loop
100 loops, best of 3: 6.56 ms per loop
1000 loops, best of 3: 368 µs per loop
Run Code Online (Sandbox Code Playgroud)

实际上np.count_nonzero赢了这个手.


Pau*_*man 10

怎么样numpy.count_nonzero:

 np.count_nonzero(df.isnull().values)   
 np.count_nonzero(df.isnull())           # also works  
Run Code Online (Sandbox Code Playgroud)

count_nonzero非常快 但是,我从一个(1000,1000)数组构建了一个数据帧,并在不同的位置随机插入了100个值,并测量了iPython中各种答案的次数:

%timeit np.count_nonzero(df.isnull().values)
1000 loops, best of 3: 1.89 ms per loop

%timeit df.isnull().values.ravel().sum()
100 loops, best of 3: 3.15 ms per loop

%timeit df.isnull().sum().sum()
100 loops, best of 3: 15.7 ms per loop
Run Code Online (Sandbox Code Playgroud)

与OP原版相比,并没有太大的时间改进,但在代码中可能不那么令人困惑,您的决定.两种count_nonzero方法(有和没有.values)之间的执行时间没有任何差别.


Con*_*anG 10

这里有很多错误的答案。OP 要求具有空值的行数,而不是列数。

这是一个更好的例子:

from numpy.random import randn
df = pd.DataFrame(randn(5, 3), index=['a', 'c', 'e', 'f', 'h'],columns=['one','two', 'three'])
df = df.reindex(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h','asdf'])
print(df)
Run Code Online (Sandbox Code Playgroud)

`现在显然有 4 行具有空值。

           one       two     three
a    -0.571617  0.952227  0.030825
b          NaN       NaN       NaN
c     0.627611 -0.462141  1.047515
d          NaN       NaN       NaN
e     0.043763  1.351700  1.480442
f     0.630803  0.931862  1.500602
g          NaN       NaN       NaN
h     0.729103 -1.198237 -0.207602
asdf       NaN       NaN       NaN
Run Code Online (Sandbox Code Playgroud)

如果您在这里使用了一些答案,您将得到 3(包含 NaN 的列数)的答案。富恩特斯的回答有效。

这是我得到它的方法:

df.isnull().any(axis=1).sum()
#4
timeit df.isnull().any(axis=1).sum()
#10000 loops, best of 3: 193 µs per loop
Run Code Online (Sandbox Code Playgroud)

'富恩特斯':

sum(df.apply(lambda x: sum(x.isnull().values), axis = 1)>0)
#4
timeit sum(df.apply(lambda x: sum(x.isnull().values), axis = 1)>0)
#1000 loops, best of 3: 677 µs per loop
Run Code Online (Sandbox Code Playgroud)


小智 9

计算行或列中缺失值的简单方法

df.apply(lambda x: sum(x.isnull().values), axis = 0) # For columns
df.apply(lambda x: sum(x.isnull().values), axis = 1) # For rows
Run Code Online (Sandbox Code Playgroud)

至少有一个缺失值的行数:

sum(df.apply(lambda x: sum(x.isnull().values), axis = 1)>0)
Run Code Online (Sandbox Code Playgroud)


ely*_*ely 5

总失踪:

df.isnull().sum().sum()
Run Code Online (Sandbox Code Playgroud)

缺少的行:

sum(map(any, df.isnull()))
Run Code Online (Sandbox Code Playgroud)