Search code examples
pythongeopandas

How to select a single value from a GeoDataFrame column?


I am trying to join a polygon of Belgium with the multipoygon of France making use of what is posted here: Make a union of polygons in GeoPandas, or Shapely (into a single geometry). Unfortunately, I am not even able to select a single (multi)polygon value from a geopandas geodataframe. It seems to behave differently from a normal pandas dataframe. The code below illustrates this (it yields an error):

import pandas as pd
import geopandas as gpd
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))

BeFra = world.loc[world['name'].isin(['France', 'Belgium'])]
BeFra['geometry'][0]

On the other hand, with a simple dataframe this method yields values:

import pandas as pd

data = [['tom', 10], ['nick', 15], ['juli', 14]] 
df = pd.DataFrame(data, columns=['Name', 'Age']) 

df['Name'][0]

How can I select a single value from a GeoDataFrame column?


Solution

  • There is no difference between a GeoDataFrame and a DataFrame for this.

    The difference in your example is that you first filter the GeoDataFrame so the element with index 0 is not in the GeoDataFrame anymore:

    print(BeFra)
    

    Results in:

            pop_est continent  ... gdp_md_est                                           geometry
    43   67059887.0    Europe  ...    2715518  MULTIPOLYGON (((-51.65780 4.15623, -52.24934 3...
    129  11484055.0    Europe  ...     533097  POLYGON ((6.15666 50.80372, 6.04307 50.12805, ...
    

    So only indexes 43 and 129 are still in the GeoDataFrame, index 0 isn't.

    If you want to select the first row regardless of the index, you can use iloc:

    import pandas as pd
    import geopandas as gpd
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    
    BeFra = world.loc[world['name'].isin(['France', 'Belgium'])]
    BeFra.iloc[0]['geometry']