查找Pandas dataFrame

时间:2016-09-22 18:15:13

标签: python pandas indexing dataframe conditional-statements

我通过阅读文本文件创建了一个数据框。我很想知道特定列中是否存在少量值,如果它们存在,我想打印整行。

这是我的输入文件(analyte_map.txt):

Analyte_id    mass    Intensity   
A34579        101.2    786788
B12345        99.2     878787
B943470       103.89   986443
C12345        11.2      101

这是我的代码:

import pandas as pd
map_file="analyte_map.txt"
array=['A34579','B943470','D583730']
analyte_df=pd.read_table(map_file,sep="\t")
for value in array:
    if analyte_df.lookup([value],['Analyte_id']):
        print '%s\t%s'%(analyte_df['mass'],analyte_df['Intensity'])

2 个答案:

答案 0 :(得分:5)

您可以boolean indexing使用isin

>>> class MyClass:
...     def __init__(self, *args):
...         for i, item in enumerate(args):
...             setattr(self, 'node_{}'.format(i+1), item)
...     def get_node(self, n):
...         return getattr(self, n)
... 
>>> x = MyClass(1, 2, 3, 4)
>>> x.get_node('node_1')
1
>>> x = MyClass('a')
>>> x.get_node('node_1')
'a'

此外,如果只需要一些列使用ix

array=['A34579','B943470','D583730']
print (df[df.analyte_id.isin(array)])
  analyte_id    mass  Intensity
0     A34579  101.20     786788
2    B943470  103.89     986443

答案 1 :(得分:2)

使用.query()方法:

In [9]: look_up=['A34579','B943470','D583730']

In [10]: df.query('Analyte_id in @look_up')
Out[10]:
  Analyte_id    mass  Intensity
0     A34579  101.20     786788
2    B943470  103.89     986443

In [11]: df.query('Analyte_id in @look_up')[['mass','Intensity']]
Out[11]:
     mass  Intensity
0  101.20     786788
2  103.89     986443