numpy:删除具有所有nan或0值的行 [英] Numpy: Drop rows with all nan or 0 values
本文介绍了numpy:删除具有所有nan或0值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果行= nan
或0
,我想从表中删除所有值.
I'd like to drop all values from a table if the rows = nan
or 0
.
我知道有一种方法可以使用熊猫来实现,即pandas.dropna(how = 'all')
,但是我想使用numpy方法删除所有nan
或0
的行.
I know there's a way to do this using pandas i.e pandas.dropna(how = 'all')
but I'd like a numpy method to remove rows with all nan
or 0
.
对此有有效的实现吗?
推荐答案
import numpy as np
a = np.array([
[1, 0, 0],
[0, np.nan, 0],
[0, 0, 0],
[np.nan, np.nan, np.nan],
[2, 3, 4]
])
mask = np.all(np.isnan(a) | np.equal(a, 0), axis=1)
a[~mask]
这篇关于numpy:删除具有所有nan或0值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文