Я думаю, вам нужна функция обработки для каждого значения столбца отдельно Series.apply
:
s = df['id_latlong'].apply(lambda x: distance_btw_coordinates(x, defined_location_latlong))
print (s)
0 1000000000
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000
6 1000000000
7 1000000000
8 1000000000
9 1000000000
10 1000000000
Name: id_latlong, dtype: int64
df.loc[s < 800]
РЕДАКТИРОВАТЬ:
Возможно ли выбрать каждое местоположение, которое находится на расстоянии менее 800 метров без необходимости хранить расстояния?
Одна идея - использовать функцию векторизации haversine_np , но необходимо изменить код для разбора строк на столбцы, а также нумерам c:
def haversine_np(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
All args must be of equal length.
"""
lon1, lat1, lon2, lat2 = map(np.radians, [lon1, lat1, lon2, lat2])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = np.sin(dlat/2.0)**2 + np.cos(lat1) * np.cos(lat2) * np.sin(dlon/2.0)**2
c = 2 * np.arcsin(np.sqrt(a))
km = 6367 * c
return km
df[['lat','long']] = df['id_latlong'].str.strip('()').str.split(';', expand=True).astype(float)
print (df)
id id_latlong lat long
0 1 (46.1988400;5.209562) 46.198840 5.209562
1 2 (46.1988400;5.209562) 46.198840 5.209562
2 3 (46.1988400;5.209562) 46.198840 5.209562
3 4 (46.1988400;5.209562) 46.198840 5.209562
4 5 (46.438805;5.11890299) 46.438805 5.118903
5 6 (46.222993;5.21707600) 46.222993 5.217076
6 7 (46.195183;5.212575) 46.195183 5.212575
7 8 (46.195183;5.212575) 46.195183 5.212575
8 9 (46.195183;5.212575) 46.195183 5.212575
9 10 (48.917459;2.570821) 48.917459 2.570821
10 11 (48.917459;2.570821) 48.917459 2.570821
lat, long = tuple(map(float, defined_location_latlong.strip('()').split(';')))
print (lat, long)
46.19884 5.209562
s = haversine_np(df['long'], df['lat'], lat, long)
print (s)
0 6016.063040
1 6016.063040
2 6016.063040
3 6016.063040
4 6037.462224
5 6017.186477
6 6015.635700
7 6015.635700
8 6015.635700
9 6353.080382
10 6353.080382
dtype: float64
#km output
df.loc[s < 0.8]
EDIT1:
Для улучшения производительности разделения можно использовать:
#550000 rows for test
df = pd.concat([df] * 50000, ignore_index=True)
df[['lat1','long1']] = pd.DataFrame([x.strip('()').split(';') for x in df['id_latlong']], index=df.index).astype(float)
df[['lat','long']] = df['id_latlong'].str.strip('()').str.split(';', expand=True).astype(float)
print (df)
In [38]: %timeit df[['lat','long']] = df['id_latlong'].str.strip('()').str.split(';', expand=True).astype(float)
2.49 s ± 722 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
In [39]: %timeit df[['lat1','long1']] = pd.DataFrame([x.strip('()').split(';') for x in df['id_latlong']], index=df.index).astype(float)
937 ms ± 11.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)