从熊猫数据框中的特定值获取索引

时间:2019-02-13 17:08:06

标签: python pandas indexing

我正在寻找一种从Dataframe列中的特定条目获取行索引的方法。我需要它,以便我可以使用该索引来获取条目所在的整行。我已经获得了在该列中寻找的值,但是无法获取它的索引。到目前为止,这是我的代码:

import glob, os
from pandas import *
filepath = r'C:\Users\Dani\Documents\clase dani\PhD\GC\Cuanti'
for csv_file in glob.glob(os.path.join(filepath, '*.csv')):
    rt=pandas.read_csv(csv_file, skiprows=6)
    rt.set_index('Peak')
    RTlist=rt.loc[:,'R.T.']
    for item in RTlist:
        if 5.5<item<5.8:
            #here's where I want to get the row index where item is at, but I can't achieve it. 

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

您可能正在寻找使用迭代项在系列RTlist中循环浏览

ctest -- --custom-arguments

答案 1 :(得分:0)

出于演示目的,我如下创建了源DataFrame(rt):

rt = pd.DataFrame([ [ 1, 2, 5 ], [ 2, 5, 6 ], [ 3, 5.52, 7 ], [ 4, 5.6, 8 ],
    [ 5, 5.75, 9 ], [ 6, 5.8, 10 ], [ 7, 6, 11 ] ],
    columns=[ 'Peak', 'R.T.', 'Xxx' ])
rt.set_index('Peak')

您可以使用指令获取结果(索引/值对):

rt['R.T.'][rt['R.T.'].between(5.5, 5.8, inclusive=False)]

打印我的测试数据:

2    5.52
3    5.60
4    5.75

rt['R.T.']选择整个'R.T.'列。

那么[rt['R.T.'].between(5.5, 5.8, inclusive=False)]是一个谓词, 选择指定范围内的值。

或者也许您仅对索引值感兴趣? 在这种情况下,请输入:

rt[rt['R.T.'].between(5.5, 5.8, inclusive=False)].index