The train station dataframe is in this format:
JavaScript
x
7
1
Station Latitude Longitude
2
0 A MRT STATION 1.440585 103.800988
3
1 B MRT STATION 1.316433 103.882906
4
2 C MRT STATION 1.369933 103.849558
5
3 D MRT STATION 1.342501 103.880178
6
4 E MRT STATION 1.281874 103.859080
7
The house dataframe is in this format
JavaScript
1
7
1
Flat Latitude Longitude
2
0 AA 1.362005 103.853880
3
1 BB 1.389245 103.876556
4
2 CC 1.372937 103.870717
5
3 DD 1.388401 103.771882
6
4 EE 1.290067 103.829246
7
i manage to find the closest station to each flat in distance, but how can i include a column which indicates what the closest station is
JavaScript
1
10
10
1
def compute_distance(flat, place):
2
Latitude, Longtitude = flat[['Latitude','Longitude']]
3
min_distance = 9999.0
4
for place_lat, place_long in zip(place['Latitude'], place['Longitude']):
5
distance = geodesic((Latitude, Longtitude), (place_lat, place_long)).m
6
if distance < min_distance:
7
min_distance = distance
8
return min_distance
9
distance_school = flat.apply(compute_distance, axis=1, place=station)
10
Here is the code that i did to find the shortest distance, but i cant seems to find a way to include the nearest train station
Advertisement
Answer
How about
JavaScript
1
11
11
1
def compute_distance(flat, place):
2
Latitude, Longtitude = flat[['Latitude','Longitude']]
3
min_distance = 9999.0
4
nearest_station = None
5
for place_lat, place_long, place_name in zip(place['Latitude'], place['Longitude'], place['Station']):
6
distance = geodesic((Latitude, Longtitude), (place_lat, place_long)).m
7
if distance < min_distance:
8
min_distance = distance
9
nearest_station = place_name
10
return min_distance, nearest_station
11