im new at python, and try to categorize places an df1 by distance to places in df2, but something gonna wrong
i have 2 dataframes whith coordinate of places
import pandas as pd
import geopy.distance
df1 = pd.DataFrame([['a', 55.88, 37.48],
['b', 55.88, 37.53],
['c', 55.89, 37.45]],
columns=['name', 'lat', 'lng']
df1 = pd.DataFrame([['f', 55.81, 37.12],
['g', 55.79, 37.23],
['h', 55.23, 37.21]],
columns=['name', 'lat', 'lng']
print(df1)
print(df2)
df1
name | lat | lng |
---|---|---|
a | 55.88 | 37.48 |
b | 55.88 | 37.53 |
c | 55.89 | 37.45 |
df2
name | lat | lng |
---|---|---|
f | 55.81 | 37.12 |
g | 55.79 | 37.23 |
h | 55.23 | 37.21 |
so, i try to calculate distance between a and f,g,h and if distance to one of this place less than 1000m, append category "close" and else category 'far', and do it for each name in df1
i want this df
print(df1)
name | lat | lng | dist_to_palce |
---|---|---|---|
a | 55.88 | 37.48 | far |
b | 55.88 | 37.53 | close |
c | 55.89 | 37.45 | far |
i try this construction
def dist(df1):
for i in range(len(df1)):
for j in range(len(df2)):
if geopy.distance.geodesic(
tuple(data[['lat','lng']].iloc[i]),
tuple(metro[['lat','lng']].iloc[j])).m <1000:
return 'close'
else: return 'far'
df1['dist_to_place'] = df1.apply(dist, axis=1)
but i got error 'float' object is not iterable
help me please :C
solution
def dist(df1_row):
for j in range(len(df2)):
if geopy.distance.geodesic(
tuple(df1_row[['lat','lng']]),
tuple(df2[['lat','lng']].iloc[j])).m <1000:
return 'close'
return 'far'
df1['dist_to_place'] = df1.apply(dist, axis=1)
import pandas as pd
import geopy.distance
df1 = pd.DataFrame({'name':['a', 'b'],'lat':[56.34, 76.56], 'lng':[23.42, 45.34]})
df2 = pd.DataFrame({'name':['f', 'g'],'lat':[56.45, 76.55], 'lng':[27.42, 40.34]})
def dist(df1_row):
for j in range(len(df2)):
if geopy.distance.geodesic(
tuple(df1_row[['lat','lng']]),
tuple(df2[['lat','lng']].iloc[j])).m <1000:
return 'close'
return 'far'
df1['dist_to_place'] = df1.apply(dist, axis=1)
far
after the cycle, not inside