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