Skip to content Skip to sidebar Skip to footer

Find Rows With Subset Of Values In Another Dataframe, In A Vectorized Way

How to match values from this DataFrame source: car_id lat lon 0 100 10.0 15.0 1 100 12.0 10.0 2 100 09.0 08.0 3 110 2

Solution 1:

DataFrame.merge() per default merges on all columns with the same names (intersection of the columns of both DFs):

In [197]: source.merge(coords)
Out[197]:
   car_id   lat   lon
0     100  12.0  10.0
1     110  23.0  12.0
2     110  18.0  32.0

Solution 2:

Here's one approach with NumPy broadcasting -

a = source.values
b = coords.values
out = source[(a[:,1:]==b[:,None]).all(-1).any(0)]

Sample run -

In [74]: source
Out[74]: 
   car_id   lat   lon
0     100  10.0  15.0
1     100  12.0  10.0
2     100   9.0   8.0
3     110  23.0  12.0
4     110  18.0  32.0
5     110  21.0  16.0
5     110  12.0   2.0

In [75]: coords
Out[75]: 
    lat   lon
0  12.0  10.0
1  23.0  12.0
3  18.0  32.0

In [76]: a = source.values
    ...: b = coords.values
    ...: 

In [77]: source[(a[:,1:]==b[:,None]).all(-1).any(0)]
Out[77]: 
   car_id   lat   lon
1     100  12.0  10.0
3     110  23.0  12.0
4     110  18.0  32.0

Post a Comment for "Find Rows With Subset Of Values In Another Dataframe, In A Vectorized Way"