23 lines
837 B
Python
23 lines
837 B
Python
import pandas as pd
|
|
import numpy as np
|
|
from geopy.distance import geodesic
|
|
|
|
def closest_city(point, df=None, population_threshold=0):
|
|
|
|
"""Get the name, population, and distance to the point of the closest city"""
|
|
|
|
if df is None:
|
|
df = pd.read_csv("africapolis.csv", index_col=0)
|
|
df = df[df["Population"] > population_threshold]
|
|
distances = df.apply(lambda row: geodesic(point, (row["Latitude"], row["Longitude"])), axis=1)
|
|
min_index = distances.idxmin()
|
|
city = df.loc[min_index]
|
|
return {
|
|
"name": city["Name"],
|
|
"population": np.int64(city["Population"]),
|
|
"distance": np.float64(distances[min_index].km),
|
|
"location": (city["Latitude"], city["Longitude"])
|
|
}
|
|
|
|
df = pd.read_csv("africapolis.csv", index_col=0)
|
|
print(closest_city((0,0), df=df, population_threshold=50000)) |