在 Pandas 数据框中的列子集中查找具有非零值的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39187788/
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
Find rows with non zero values in a subset of columns in pandas dataframe
提问by Amrith Krishna
I have a datframe with 4 columns of strings and others as integers. Now I need to find out those rows of data where at least one of the column is a non-zero value (or > 0).
我有一个包含 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
My output should be
我的输出应该是
manwra, akriti,T5,0,0,1,0,K
awma, prabrtih,B6, 0,1,1,0,S
I have tried the following to obtain the answer. The string values are in colums 0,1,2 and -1 (last column).
我尝试了以下方法来获得答案。字符串值位于 0,1,2 和 -1(最后一列)列中。
KT[KT.ix[:,3:-2] != 0]
What I am receiving as output is
我收到的输出是
NaN,NaNNaN,NaN,NaN,NaN,NaN,NaN
NaN,NaN,NaN,NaN,NaN,1,NaN,NaN
NaN,NaN,NaN,NaN,1,1,NaN,NaN
How to obtain the desired output
如何获得所需的输出
采纳答案by MaxU
Here is an alternative solution which uses select_dtypes()method:
这是使用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
Explanation:
解释:
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
回答by piRSquared
回答by Merlin
You were close:
你很接近:
#your's
KT[KT.ix[:,3:-2] != 0]
#works
KT[(KT.ix[:,3:6] > 0).any(1)]
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
#key diff
(KT.ix[:,3:6] > 0)
3 4 5 6
0 False False False False
1 False False True False
2 False True True False