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
010012.010.0111023.012.0211018.032.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
010010.015.0110012.010.021009.08.0311023.012.0411018.032.0511021.016.0511012.02.0In [75]: coords
Out[75]: 
    lat   lon
012.010.0123.012.0318.032.0In [76]: a = source.values
    ...: b = coords.values
    ...: 

In [77]: source[(a[:,1:]==b[:,None]).all(-1).any(0)]
Out[77]: 
   car_id   lat   lon
110012.010.0311023.012.0411018.032.0

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