I'm looking for a way to get the row index from an specific entry in that's in a column of the Dataframe. I need it so that the I can use that index to get the whole row where that entry is. I have achieved to get the value I'm looking for in the column but I'm unable to get its index. Here's my code so far:
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.
Thanks for your help.
Making use of iteritems to loop through the Series RTlist, might be what you're looking for
for index, item in RTlist.iteritems():
if 5.5<item<5.8:
item_index = index
item_value = item
For demonstration purpose I created the source DataFrame ( rt
) as follows:
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')
You can get the result (index / value pairs) with a single instruction:
rt['R.T.'][rt['R.T.'].between(5.5, 5.8, inclusive=False)]
For my test data it prints:
2 5.52
3 5.60
4 5.75
rt['RT']
selects a whole 'RT'
column.
Then [rt['RT'].between(5.5, 5.8, inclusive=False)]
is a predicate, selecting values in the specified range.
Or maybe you are interested in index values only ? If this is the case, write:
rt[rt['R.T.'].between(5.5, 5.8, inclusive=False)].index
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.