python - select rows in pandas DataFrame using comparisons against two columns -


i have pandas dataframe:

df = pd.dataframe({'one' : [1, 2, 3, 4] ,'two' : [5, 6, 7, 8]})    1  2 0    1    5 1    2    6 2    3    7 3    4    8 

column "one" , column "two" comprise (x,y) coordinates

lets have list of coordinates: c = [(1,5), (2,6), (20,5)]

is there elegant way of obtaining rows in df matching coordinates? in case, given c, matching rows 0 , 1

related question: using pandas select rows using 2 different columns dataframe?

and: selecting rows pandas dataframe using 2 columns

i don't know elegance, seems work.

first find xy coordinates dataframe:

xy = [(o, t) (_, o, t) in df[['one', 'two']].to_records()] 

now find indices in c:

inds = [i (i, xy_) in enumerate(xy) if xy_ in set(c)] 

finally, return dataframe's rows these indices:

df.ix[inds] 

Comments