So I have a Dataset like this:
Customer_id Lat Lon
0. A 40 12
1. A np.nan np.nan
2. A np.nan np.nan
3. A 43 12
4. A 45 13
5. B 43 14
6. B np.nan np.nan
7. B 43 16
Where the coordinates (40,12),(43,12),(45,13),(43,14) and (43,16) are the cell towers of a certain network.
Then I apply some interpolation functions and it results in something like the following:
Customer_id Lat Lon
0. A 40 12
1. A 41 12
2. A 42 12
3. A 43 12
4. A 45 13
5. B 43 14
6. B 43 15
7. B 43 16
But these new coordinates are just estimates and not actual towers. I would like to then assign these estimations to the closest actual tower so that, for example, record 1 would be assigned to the tower (40,12).
I used this code
def haversine_closest_changed(towers, row):
all_points= towers
lat2= all_points[:,0] #the actual latitudes of the towers
lon2= all_points[:,1] #the actual longitudes of the towers
l=len(lat2) #how many towers are there
lat1=row['Expected_Lat'] #make a column with the actual latitude my value and all the towers,
#the point I'm looking at multiple times
lon1=row['Expected_Lon'] #find the min distance and output the minimum
lat1, lon1, lat2, lon2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlat = lat2 - lat1
dlon = lon2 - lon1
a = np.sin(dlon/2.0)**2 + np.cos(lon1) * np.cos(lon2) * np.sin(dlat/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
idx=np.argmin(km)
closest_point=towers[idx,]
return closest_point
where towers is a pandas dataset with all the towers that exist in the network (one column for Latitude and another for Longitude) and the columns Expected_Lat and Expected_Lon are what I called the columns after I did the interpolation.
This piece of code returns me only 1 value for the latitude and 1 value for the longitude repeated throughout the whole column. How can I change this code to replace only the points that I have interpolated/the points that were previously NaNs by the closest tower?