1. import pandas as pd
  2. from geopy.geocoders import Nominatim
  3. import folium
  4. #Load dataset
  5. data = pd.read_excel("data.xlsx")
  6. geolocator = Nominatim(user_agent="my_geocoder")
  7. def geocode_address(row):
  8. location = geolocator.geocode(f"{row['City']}, {row['State_or_Province']}, {row['Country']}")
  9. if location:
  10. return location.latitude, location.longitude
  11. else:
  12. return None, None
  13. data['Latitude'], data['Longitude'] = zip(*data.apply(geocode_address, axis=1))
  14. #Calculate mean of coordinates for map center
  15. map_center = [data['Latitude'].mean(), data['Longitude'].mean()]
  16. #make map with folium
  17. mymap = folium.Map(location=map_center, zoom_start=4) #adjust as needed
  18. #Add markers
  19. for index, row in data.iterrows():
  20. if row['Latitude'] and row['Longitude']:
  21. folium.Marker([row['Latitude'], row['Longitude']], popup=row['City']).add_to(mymap)
  22. #Save
  23. mymap.save("geocoded_map.html")