I have a Pandas dataframe containing several columns of attributes describing various locations. Let us call the dataframe df, and the dataframe has one column location_tuple containing a tuple of (latitude, longitude) coordinates values for every row in df. I would now like to create a new column in df containing the city name for each of the locations in df, and wonder if anyone could suggest a convenient approach to do this.
I am able to get the city name of a given row in df using the geopy package. Importing Nominatim
from geopy.geocoders
and creating a geolocator object as geolocator = Nominatim(user_agent="myapp")
, I can get the city name of the coordinates on row zero by typing
geolocator.reverse(df.location_tuple[0]).raw['address']['city']
but I find no way in implementing this to get a new column with city names for all rows in the dataframe. I would appreciate some help on this.
Many thanks in advance!
A
lambda
expression is what you need to describe the entire process of gettingcity
fromlocation_tuple
.Inserting this into either
list(map())
ordf.apply()
will work.Code: (please provide sample data next time for the helper's convenience)
Output:
Also consider adding
language="en"
ingeolocator.reverse()
so the city name becomes English.