How to update the attribute of a GeoDataFrame based on another database? I have a master GeoDataFrame with line features. I have another GeoDataFrame (say obs) which is a subset of the master geodatabase. My analysis regularly updates the obs. How can I write the results back to the master GeoDataFrame by matching geometry?

Can I do spatial join using geometry as the key?

GeoPandas documentation talks about spatial join by merging. How can I use this approach for updating my attribute values?

  • Do your updates change the obs geometry or other related values? – Adelson Araújo Apr 23 '20 at 1:34
  • No. Just the attribute values of a subset of master GeoDataset – PPR Apr 23 '20 at 2:06

I can't see where spatial join would apply for your case. But if you have a "master" dataframe and a subset of it updating, you can write the updates back with pandas .loc[].

master = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
obs = master.copy().sample(3)
obs['gdp_md_est'] = obs['gdp_md_est']**2         # updating some other values
obs['geometry'] = obs['geometry'].simplify(0.5)  # updating geometry
master.loc[obs.index] == obs  # see that different after the update (should return False)
master.loc[obs.index] = obs   # update
master.loc[obs.index] == obs  # check if your updates were completed (should return True)
  • Thanks a lot. A quick follow-up question. If obs data has fewer columns (or attributes) than the master data can I use the following to update my model? master.loc[obs.index,[list of attribute columns]= curr_obs. This works for my present case. – PPR Apr 23 '20 at 3:14
  • Yes, your solution is fine. – Adelson Araújo Apr 23 '20 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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