I have a dataframe with a columns for Country and Coordinates (country_gdf). I have a seperate dataframe with a columns for City, Latitude, and Longitude (cities_gpd).
I want to add a new column to the Country dataframe for a count of the number of cities within a country. But, I'm not sure of how to work with coordinates
My sample data is as follows;
import pandas as pd
import geopandas as gpd
from shapely import wkt
# Sample Country dataframe
country = pd.DataFrame(
{'Country': ['Argentina', 'Brazil', 'Chile'],
'Coordinates': ['POINT(-58.66000 -34.58000)', 'POINT(-47.91000 -15.78000)',
'POINT(-70.66000 -33.45000)']})
country['Coordinates'] = gpd.GeoSeries.from_wkt(country['Coordinates'])
country_gdf = gpd.GeoDataFrame(country, geometry='Coordinates')
# Sample Cities Dataframe
cities = pd.DataFrame(
{'City': ['Buenos Aires', 'Brasilia', 'Santiago'],
'Latitude': [-38.4161, -14.2350, -33.4489],
'Longitude': [-63.6167, -51.9253, -70.6693]})
cities_gpd = gpd.GeoDataFrame(cities,
geometry=gpd.points_from_xy(cities.Longitude,
cities.Latitude))
I have tried
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
polygon= country_gdf['Coordinates']
point = cities_gpd['geometry']
print(polygon.contains(point))
but the output is wrong