如果一列中的至少值满足使用Pandas any的条件,则选择行

时间:2015-08-31 00:35:03

标签: python pandas

我有以下数据框:

import pandas as pd
df = pd.DataFrame({ 'gene':["foo","bar","qux","woz"], 'cell1':[5,0,1,0], 'cell2':[12,90,13,0]})
df = df[["gene","cell1","cell2"]]

看起来像这样:

  gene  cell1  cell2
0  foo      5     12
1  bar      0     90
2  qux      1     13
3  woz      0      0

我想要做的是根据第二列以后的至少一个值是否大于2来选择行。产生这个:

  gene  cell1  cell2
0  foo      5     12
1  bar      0     90
2  qux      1     13

我试过了,但它并没有给我我想要的东西:

df[(df.values > 2).any(axis=1)]

做正确的方法是什么?

2 个答案:

答案 0 :(得分:3)

您应该选择cell1cell2,然后针对2进行检查。示例 -

In [4]: df[(df[['cell1','cell2']] > 2).any(axis=1)]
Out[4]:
   cell1  cell2 gene
0      5     12  foo
1      0     90  bar
2      1     13  qux

答案 1 :(得分:0)

怎么样:

df[(df.cell1 > 2).any(axis=1)]

在第二栏中,我不确定你是在谈论cell1还是cell2 ......

相关问题