Google Map API
Step 1: Install geopy
pip install geopy
step 2: read the Longitude and latitude from CSV
import pandas as pd
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
# Load the dataset
data = pd.read_csv('locations.csv') # Replace with your CSV file name
# Initialize the geolocator
geolocator = Nominatim(user_agent="geoapiExercises")
# Apply a rate limiter to avoid being blocked by the service
geocode = RateLimiter(geolocator.reverse, min_delay_seconds=1)
# Function to get the location name
def get_location_name(row):
try:
location = geocode((row['latitude'], row['longitude']))
return location.address
except:
return "Location not found"
# Apply the function to each row
data['location_name'] = data.apply(get_location_name, axis=1)
# Print the DataFrame with location names
print(data[['longitude', 'latitude', 'location_name']])
# Optionally, save the updated DataFrame to a new CSV
data.to_csv('locations_with_names.csv', index=False)
Comments