title: With a dataframe that contains coordinates, find other rows with coordinates within n miles of each row
body: <p>I've been going around in circles and I can't figure out the right way to do this.</p>
  <p>Ok so I have several dataframes. Within each dataframe I have coordinates in column <code>d['LATITUDE']</code> and <code>d['LONGITUDE']</code>. I want to figure out how many other rows have coordinates within n miles of each row. I created a few new columns to store the counts but actually filing them has me stumped. I've read its not good to iterate over rows in a dataframe, but I can't figure out how to do this a better way. It seems to work but takes hours for each dataframe.</p>
  <pre><code>import numpy as np
  import pandas as pd
  from geopy.distance import geodesic
  
  #example input
       LATITUDE  LONGITUDE
  0   38.954700  -77.04630
  1   38.940400  -77.04800
  2   38.903200  -77.14170
  3   38.864000  -77.15600
  4   38.863900  -77.16310
  5   38.901700  -77.13100
  6   38.947783  -77.03983
  7   38.862900  -77.15580
  8   38.947800  -77.04390
  9   38.901700  -77.13100
  10  38.949100  -77.04610
  11  38.864300  -77.15390
  12  38.864300  -77.15390
  13  38.946400  -77.03850
  14  38.903000  -77.14110
      
      
  d['1_mile'] = np.nan
  d['5_miles'] = np.nan
  d['10_miles'] = np.nan
      
          
  for n, r in enumerate(d.itertuples(), 1):
          df = d.apply(lambda row: geodesic((d['LATITUDE'].iloc[n], d['LONGITUDE'].iloc[n]), (row['LATITUDE'], row['LONGITUDE'])).miles, axis=1)
  
          c = len(df[(df&lt;=1) &amp; (df&gt;0)])
          d.at[n,'1_mile'] = c
  
          c = len(df[(df&lt;=5) &amp; (df&gt;0)])
          d.at[n,'5_miles'] = c
  
          c = len(df[(df&lt;=10) &amp; (df&gt;0)])
          d.at[n,'10_miles'] = c
  
  #desired output
       LATITUDE  LONGITUDE  1_mile  5_miles  10_miles
  0   38.954700  -77.04630     5.0      5.0      14.0
  1   38.940400  -77.04800     5.0      5.0      14.0
  2   38.903200  -77.14170     3.0      8.0      14.0
  3   38.864000  -77.15600     4.0      8.0      14.0
  4   38.863900  -77.16310     4.0      8.0      14.0
  5   38.901700  -77.13100     2.0      7.0      13.0
  6   38.947783  -77.03983     5.0      5.0      14.0
  7   38.862900  -77.15580     4.0      8.0      14.0
  8   38.947800  -77.04390     5.0      5.0      14.0
  9   38.901700  -77.13100     2.0      7.0      13.0
  10  38.949100  -77.04610     5.0      5.0      14.0
  11  38.864300  -77.15390     3.0      7.0      13.0
  12  38.864300  -77.15390     3.0      7.0      13.0
  13  38.946400  -77.03850     5.0      5.0      14.0
  14  38.903000  -77.14110     3.0      8.0      14.0
  </code></pre>
  <p>It works but I know it's not the best way to do this. Can anyone please help me clean it up?</p>
  <p>Thank you!</p>