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
# 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