我有一个datframe,其中包含4列字符串,其他列为整数。现在,我需要找出那些数据行,其中至少一列是非零值(或> 0)。
manwra,sahAyaH,T7,0,0,0,0,T
manwra, akriti,T5,0,0,1,0,K
awma, prabrtih,B6, 0,1,1,0,S
我的输出应该是
manwra, akriti,T5,0,0,1,0,K
awma, prabrtih,B6, 0,1,1,0,S
我尝试了以下方法以获得答案。字符串值在第0、1、2和-1列(最后一列)中。
KT[KT.ix[:,3:-2] != 0]
我收到的输出是
NaN,NaNNaN,NaN,NaN,NaN,NaN,NaN
NaN,NaN,NaN,NaN,NaN,1,NaN,NaN
NaN,NaN,NaN,NaN,1,1,NaN,NaN
如何获得所需的输出
这是使用select_dtypes()方法的替代解决方案:
In [41]: df[(df.select_dtypes(include=['number']) != 0).any(1)]
Out[41]:
0 1 2 3 4 5 6 7
1 manwra akriti T5 0 0 1 0 K
2 awma prabrtih B6 0 1 1 0 S
解释:
In [42]: df.select_dtypes(include=['number']) != 0
Out[42]:
3 4 5 6
0 False False False False
1 False False True False
2 False True True False
In [43]: (df.select_dtypes(include=['number']) != 0).any(1)
Out[43]:
0 False
1 True
2 True
dtype: bool
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句