1

I'd like to check if a point in one dataframe exists in another using Python. I have two shapefiles / GeoPandas dataframes with each over 10000 rows, and the geometry is stored as points (eg. POINT (3.14159265359 2.71828182846)). I now would like to add a column to the first dataframe, and populate it with a certain value if this point happens to be in the other dataframe.

Using .isin() works, yet very slowly compared to gpd.sjoin(foodf, bardf). I assume that .isin() brute-forces its way, whereas the latter uses indices. (Needless to say and understandably, gpd.sjoin omits rows which are not joined.)

I would like to know, how I could speed up the process. All I need is to assign 1 for points which exist in both datasets and 0 for other cases. Is there a function in GeoPandas (or another package) included helping me, or would you recommend me to use an rtree index?

import geopandas as gpd

foodf = gpd.read_file("C:\\monty.shp")
bardf = gpd.read_file("C:\\python.shp")

foodf['centroid_exists'] = foodf.centroid.isin(bardf.centroid).astype(int)
# it works, yet very slowly

0

1 Answer 1

2

Spatial join should work. You must have been using 'inner' join type?

‘inner’: use intersection of keys from both dfs; retain only left_df geometry column

how='left' should give you the results you want:

df = gpd.sjoin(foodf, baadf, how='left')

I just tried it on two df with ~200k Points in each and it finishes in 30 seconds:

enter image description here

1
  • 2
    Thanks, it indeed works! In fact so well, that I feel a bit humbled about the ease of the solution...
    – skna.1000
    Commented Apr 8, 2020 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.