在 pandas 中找到多个具有特定值的列

locate several column with specific values in pandas

你好,我知道怎么用

`df2.loc[df2[2]=="gene"]` for instance 

但假设我从中得到:

                0         1     2        3        4     5  6  7   8
0  NW_011626968.1  AUGUSTUS  gene  1671821  1675615  0.04  -  .  g1
7  NW_011626968.1  AUGUSTUS  gene  1677196  1679599  0.13  +  .  g2

如何添加 col2 和 3 的信息以匹配特定值并仅获取第 7 行?

我试过了:

df2.loc[df2[2]=="gene"]  & df2.loc[df2[3]=="1677196"] & df2.loc[df2[4]=="1679599"]

这里是必要的链掩码,未过滤的行,也添加了()因为运算符的优先级:

#if columns are filled by strings
#df3 = df2[(df2[2]=="gene") & (df2[3]=="1677196") & (df2[4]=="1679599")]

#if columns are filled by numbers
df3 = df2[(df2[2]=="gene") & (df2[3]==1677196) & (df2[4]==1679599)]
print (df3)
                0         1     2        3        4     5  6  7   8
7  NW_011626968.1  AUGUSTUS  gene  1677196  1679599  0.13  +  .  g2