Python, Pandas hvplot, API
# Configure the map
map_plot_1 = city_data_df.hvplot.points(
"Lng",
"Lat",
geo = True,
tiles = "OSM",
color = "City",
scale = 1,
alpha = 0.5,
frame_width = 700,
frame_height = 500
)
# Display the map plot
map_plot_1
# Narrow down cities that fit criteria and drop any results with null values
filtered_weather_df = city_data_df.loc[
(city_data_df["Max Temp"] < 27) &
(city_data_df["Max Temp"] > 21) &
(city_data_df["Wind Speed"] < 4.5) &
(city_data_df["Cloudiness"] == 0)
]
# Drop any rows with null values
filtered_weather_df = filtered_weather_df.dropna()
# Display sample data
filtered_weather_df
Hotel Search
# Print a message to follow up the hotel search
print("Starting hotel search")
# Iterate through the hotel_df DataFrame
for index, row in hotel_df.iterrows():
lat = row["Lat"]
lon = row["Lng"]
# Add filter and bias parameters with the current city's latitude and longitude to the params dictionary
params["filter"] = f"circle:{lon},{lat},{radius}"
params["bias"] = f"proximity:{lon},{lat}"
# Set base URL
base_url = "https://api.geoapify.com/v2/places"
# Make and API request using the params dictionary
name_address = requests.get(base_url, params=params)
# Convert the API response to JSON format
name_address = name_address.json()
# Grab the first hotel from the results and store the name in the hotel_df DataFrame
try:
hotel_df.loc[index, "Hotel Name"] = name_address["features"][0]["properties"]["name"]
except (KeyError, IndexError):
# If no hotel is found, set the hotel name as "No hotel found".
hotel_df.loc[index, "Hotel Name"] = "No hotel found"
# Log the search results
print(f"{hotel_df.loc[index, 'City']} - nearest hotel: {hotel_df.loc[index, 'Hotel Name']}")
# Display sample data
hotel_df
Results
![image](https://private-user-images.githubusercontent.com/126124356/253826118-1f9a0028-d861-4bd3-bdf8-4cdd593b4063.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA4ODgyOTQsIm5iZiI6MTcyMDg4Nzk5NCwicGF0aCI6Ii8xMjYxMjQzNTYvMjUzODI2MTE4LTFmOWEwMDI4LWQ4NjEtNGJkMy1iZGY4LTRjZGQ1OTNiNDA2My5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzEzJTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcxM1QxNjI2MzRaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT1mYjFjNDkwM2VkYjdhYjlhYzM2YjNmNDlmOWY2N2RhYzQ4MDMxN2I1ZDIxZjVhYWQzOGE0MGMyYzA3YTBmYjk5JlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.ZT-2Op9kQESzOAXSJJ21ZxgtckDh6CPuaAK71cl7h9Q)
# Configure the map
map_plot_2 = city_data_df.hvplot.points(
"Lng",
"Lat",
geo = True,
tiles = "OSM",
color = "City",
scale = 1,
alpha = 0.5,
frame_width = 700,
frame_height = 500,
hover_cols = ["Hotel Name", "Country"],
size = "Humidity"
)
# Display the map plot
map_plot_2
Please refer to the following:
My LinkedIn Page
My Email Contact