2

I have 2 pandas data frames, restaurants:

     Lat        Long             Name
0    43.599503  1.440678      Le Filochard
1    43.602369  1.447368      Le Wallace
2    43.603838  1.435186      Chez Tonton, Pastis Ô Maître

and hotels:

      Lat      Long             Name
0   43.603779  1.444004         Grand Hôtel de l'Opéra
1   43.599482  1.441207         Hôtel Garonne
2   43.549924  1.499821         Ibis Styles

And 1 function distance(origin,destination) with origin = (lat,long) coord.

I am trying to apply the function to the 2 data frames to calculate for each row of the first data frame the number of item from the second data frame for which the distance is inferior to 0.2 ...

I'm trying the apply map function but do not manage to perform it with the 2 data frames; do I need to merge them?

JohnE
  • 29,156
  • 8
  • 79
  • 109
philippeZ
  • 23
  • 4

1 Answers1

1

Would this work for you? Just need to correct function for distance

hotels=pd.DataFrame([[43.599503, 1.440678, 'Le Filochard'],
                     [43.602369, 1.447368, 'Le Wallace'],
                     [43.603838, 1.435186, 'Chez Tonton, Pastis Ô Maître']],
                   columns=['Lat', 'Long', 'Name'])
restaurants=pd.DataFrame([[43.603779, 1.444004, "Grand Hôtel de l'Opéra"],
                          [43.599482, 1.441207, 'Hôtel Garonne'],
                          [43.549924, 1.499821, 'Ibis Styles']],
                         columns=['Lat', 'Long', 'Name'])
hotels['Nearby Resaurants'] = hotels.apply(lambda h: list(restaurants[((restaurants.Lat-h.Lat)**2+(restaurants.Long-h.Long)**2)<0.005].Name), axis=1)
print(hotels)
            Lat      Long                          Name  \
0  43.599503  1.440678                  Le Filochard   
1  43.602369  1.447368                    Le Wallace   
2  43.603838  1.435186  Chez Tonton, Pastis Ô Maître   

                         Nearby Resaurants  
0  [Grand Hôtel de l'Opéra, Hôtel Garonne]  
1  [Grand Hôtel de l'Opéra, Hôtel Garonne]  
2  [Grand Hôtel de l'Opéra, Hôtel Garonne]

EDIT:

Modification to handle function, filtering of hotels also by using lambda function

def distance(X,Y):
    return((X[0]-X[1])**2+(Y[0]-Y[1])**2)

hotels['Nearby Resaurants'] = hotels.apply(lambda h: list(restaurants.loc[restaurants.apply(lambda r: distance((r.Lat,h.Lat),(r.Long,h.Long))<0.005, axis=1)].Name), axis=1)
print(hotels)
Sergey
  • 661
  • 5
  • 6
  • Thanks Sergey, it's working pretty well with the formula directly. But I cannot perform the same operation with my function distance((xlat,xlong),(ylat,ylong): I'm tring this: `code`df_hotel['Nearby Resto'] = df_hotel.apply(lambda h: list(df_resto[(distance((df_resto.Lat,h.Lat),(df_resto.Long,h.Long))<0.000005)].Name), axis=1)`code` – philippeZ Sep 19 '18 at 11:19
  • 1
    Its working !! my headache is slowly decreasing. Thank you very much. – philippeZ Sep 19 '18 at 11:49