Python 如果一列中至少有一个值满足使用“任意”的条件,则选择行
我有以下数据框:Python 如果一列中至少有一个值满足使用“任意”的条件,则选择行,python,pandas,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
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列之后的至少一个值是否大于2来选择行。因此:
gene cell1 cell2
0 foo 5 12
1 bar 0 90
2 qux 1 13
我试过了,但它没有给我想要的:
df[(df.values > 2).any(axis=1)]
正确的方法是什么?您应该选择
cell1
和cell2
,然后对照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
那么:
df[(df.cell1 > 2).any(axis=1)]
到了第二篇专栏,我不确定你们是在谈论cell1还是cell2