可以通过以下步骤实现:
import pandas as pd
from geopy.distance import geodesic
city_df = pd.DataFrame({'City': ['City A', 'City B', 'City C'],
'Latitude': [40.7128, 34.0522, 51.5074],
'Longitude': [-74.0060, -118.2437, -0.1278]})
airport_df = pd.DataFrame({'Airport': ['Airport X', 'Airport Y', 'Airport Z'],
'Latitude': [40.6413, 34.0522, 51.4700],
'Longitude': [-73.7781, -118.2437, -0.4543]})
def calculate_distance(city_lat, city_lon, airport_lat, airport_lon):
city_coords = (city_lat, city_lon)
airport_coords = (airport_lat, airport_lon)
distance = geodesic(city_coords, airport_coords).miles
return distance
for _, city_row in city_df.iterrows():
city = city_row['City']
city_lat = city_row['Latitude']
city_lon = city_row['Longitude']
for _, airport_row in airport_df.iterrows():
airport = airport_row['Airport']
airport_lat = airport_row['Latitude']
airport_lon = airport_row['Longitude']
distance = calculate_distance(city_lat, city_lon, airport_lat, airport_lon)
if distance <= 50: # 假设距离小于等于50英里为给定城市距离内的机场
print(f"The airport {airport} is within 50 miles of {city}.")
这段代码的功能是循环遍历城市数据帧和机场数据帧,计算给定城市距离内的机场。首先,通过导入所需的库和模块,包括Pandas和geopy.distance中的geodesic函数。然后,创建两个数据帧,一个包含城市信息,另一个包含机场信息。接下来,定义了一个计算城市距离的函数,该函数使用geodesic函数计算两个坐标之间的距离。最后,通过嵌套循环遍历城市数据帧和机场数据帧,并应用计算距离的函数。如果距离小于等于50英里,则打印出机场在给定城市距离内的信息。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云