选择只有一个负值的列 [英] Select column with only one negative value
本文介绍了选择只有一个负值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想选择只有一个负值或没有负值的列.在这个例子中,我该如何构造呢?我一直在寻找类似的东西,但是没有成功.感谢您的帮助.
I'd like to select the columns that only have one negative value or none. How can I construct this looking at this example? I've been searching for something similar, didn't succeed though. Thanks for any help.
N = 5
np.random.seed(0)
df1 = pd.DataFrame(
{'X':np.random.uniform(-3,3,N),
'Y':np.random.uniform(-3,3,N),
'Z':np.random.uniform(-3,3,N),
})
X Y Z
0 0.292881 0.875365 1.750350
1 1.291136 -0.374477 0.173370
2 0.616580 2.350638 0.408267
3 0.269299 2.781977 2.553580
4 -0.458071 -0.699351 -2.573784
因此在此示例中,我想返回X和Z列.
So in this example I'd like to return columns X and Z.
推荐答案
您可以使用iloc来实现
You can use iloc to do that i.e
df1.iloc[:,((df1<0).sum(0) <= 1).values]
或(感谢乔恩)
df1.loc[:,df1.lt(0).sum() <= 1]
输出:
X Z
0 0.292881 1.750350
1 1.291136 0.173370
2 0.616580 0.408267
3 0.269299 2.553580
4 -0.458071 -2.573784
这篇关于选择只有一个负值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文