Search code examples
pythonpandasmergelatitude-longitude

Need to merge two pandas dataframe using two columns latitude and longitude


this is my dataframe#1 :city names with its latitude and longitude

df1 = {"city":['delhi','new york','london','paris','chennai'],"lat":[12.23,22.444,23.233,45.32,34.22],"long":[11.22,22.332,34.23,55.23,24.22]

this is dataframe#2 : country names with latitude and longitude

df2 = pd.DataFrame({"country":['India','US','UK','France','India'],"lat":[12.13,22.54,22.33,45.32,34.22],"long":[11.12,22.132,34.23,54.23,24.22]})

I need to match these two columns lat and long to merge these two tables. the problem is the lat and long is not exactly matching and the values are + or - 0.1 or 0.2. (if matched I can use the pd.merge option) lat and longs are not real here. just an example

Expected Result:

result = pd.DataFrame({"city":['delhi','new york','london','paris','chennai'],"country":['India','US','UK','France','India'],"lat":[12.13,22.54,22.33,45.32,34.22],"long":[11.12,22.132,34.23,54.23,24.22]})

what is the best approach to merge these tables?


Solution

  • For example of a cross merge:

    (df1.assign(dummy=1)
        .merge(df2.assign(dummy=1),on='dummy')
        .query('abs(lat_x-lat_y)<=0.1 and abs(long_x-long_y)<=0.2')
        .drop('dummy', axis=1)
    )
    

    Output:

            city   lat_x  long_x country  lat_y  long_y
    0      delhi  12.230  11.220   India  12.13  11.120
    6   new york  22.444  22.332      US  22.54  22.132
    24   chennai  34.220  24.220   India  34.22  24.220