pandas Python - 如果两列是 NaN 则删除行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39128856/
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
Python - Drop row if two columns are NaN
提问by Kevin M
This is an extension to this question, where OP wanted to know how to drop rows where the values in a single column are NaN.
这是这个问题的扩展,其中 OP 想知道如何删除单列中的值为 NaN 的行。
I'm wondering how I can drop rows where the values in 2(or more) columns are bothNaN. Using the second answer's created Data Frame:
我想知道如何删除2(或更多)列中的值都是NaN 的行。使用第二个答案创建的数据框:
In [1]: df = pd.DataFrame(np.random.randn(10,3))
In [2]: df.ix[::2,0] = np.nan; df.ix[::4,1] = np.nan; df.ix[::3,2] = np.nan;
In [3]: df
Out[3]:
0 1 2
0 NaN NaN NaN
1 2.677677 -1.466923 -0.750366
2 NaN 0.798002 -0.906038
3 0.672201 0.964789 NaN
4 NaN NaN 0.050742
5 -1.250970 0.030561 -2.678622
6 NaN 1.036043 NaN
7 0.049896 -0.308003 0.823295
8 NaN NaN 0.637482
9 -0.310130 0.078891 NaN
If I use the drop.na()
command, specifically the drop.na(subset=[1,2])
, then it completes an "or" type drop and leaves:
如果我使用drop.na()
命令,特别是drop.na(subset=[1,2])
,那么它会完成一个“或”类型的 drop 并离开:
In[4]: df.dropna(subset=[1,2])
Out[4]:
0 1 2
1 2.677677 -1.466923 -0.750366
2 NaN 0.798002 -0.906038
5 -1.250970 0.030561 -2.678622
7 0.049896 -0.308003 0.823295
What I want is an "and" type drop, where it drops rows where there is an NaN
in column index 1 and2. This would leave:
我想要的是“和”类型的删除,它删除NaN
列索引 1和2所在的行。这将留下:
0 1 2
1 2.677677 -1.466923 -0.750366
2 NaN 0.798002 -0.906038
3 0.672201 0.964789 NaN
4 NaN NaN 0.050742
5 -1.250970 0.030561 -2.678622
6 NaN 1.036043 NaN
7 0.049896 -0.308003 0.823295
8 NaN NaN 0.637482
9 -0.310130 0.078891 NaN
where only the first row is dropped.
只有第一行被删除。
Any ideas?
有任何想法吗?
EDIT: changed data frame values for consistency
编辑:更改数据框值以保持一致性
回答by Alberto Garcia-Raboso
Any one of the following two:
以下两项中的任何一项:
df.dropna(subset=[1, 2], how='all')
or
或者
df.dropna(subset=[1, 2], thresh=1)