Python 大于阈值的列
如何检索中至少出现一次值Python 大于阈值的列,python,pandas,threshold,Python,Pandas,Threshold,如何检索中至少出现一次值
THRESHOLD = 0
print(df)
Col_1 Col_2 Col_3 Col_4
1 3 5 -9
1 3 5 -9
1 -2 5 -9
打印(最终版)
我试过:
df[(df < 0).any(1)]
df[(df<0).任何(1)]
但它报告至少有一个元素<0出现的行,而不是列。使用
axis=0
和一起使用
df.loc[:,(df < 0).any(0)]
Out[215]:
Col_2 Col_4
0 3 -9
1 3 -9
2 -2 -9
您可以发布
df.loc[:,(df<0.any)]
然后
df.loc[:,(df<0).any(0)]
选择df<0的所有行和列。any(0)
通过布尔索引为True
。您可以转置,但这可能不是最佳解决方案<代码>df.T[(df.T<0).any(1)].T
df.loc[:,(df < 0).any(0)]
Out[215]:
Col_2 Col_4
0 3 -9
1 3 -9
2 -2 -9
df.iloc[:,(df<0).any().nonzero()[0]]
Out[230]:
Col_2 Col_4
0 3 -9
1 3 -9
2 -2 -9
>>> df
Col_1 Col_2 Col_3 Col_4
0 1 3 5 -9
1 1 3 5 -9
2 1 -2 5 -9
>>>
>>> df.loc[:, (df < 0).any(0)]
Col_2 Col_4
0 3 -9
1 3 -9
2 -2 -9
>>> df < 0
Col_1 Col_2 Col_3 Col_4
0 False False False True
1 False False False True
2 False True False True
>>>
>>> (df < 0).any(0)
Col_1 False
Col_2 True
Col_3 False
Col_4 True
dtype: bool