title: Python Extreme Slow iterrows
body: <p>I'm not a good coder in python and need help to change my iterrows function into some other stuff.</p>
  <p>So -- I have two data frames, one is zips with lat &amp; lng, another one is a bunch of locations with lat &amp; lng. I need to map each zip with all the locations and calculate the distance between them.</p>
  <p><strong>zip</strong> Table:</p>
  <pre><code>|  zip  |  lat  |  lng   |
  | 10001 | 40.81 | -73.04 |
  | 10029 | 21.59 | -70.25 |
  | 20033 | 30.21 | -52.11 |
  | 30005 | 50.33 | -75.89 |
  | 10203 | 40.23 | -30.21 |
  </code></pre>
  <p><strong>location</strong> Table:</p>
  <pre><code>|location|location_lat|location_lng|
  |    A   |    40.78   |   -74.44   |
  |    B   |    38.69   |   -90.40   |
  |    C   |    42.64   |   -73.78   |
  </code></pre>
  <p>Desired Output:</p>
  <pre><code>|  zip  |  lat  |  lng   | location |location_lat|location_lng| distance |
  | 10001 | 40.81 | -73.04 |    A     |    40.78   |   -74.44   |   30000  |
  | 10001 | 40.81 | -73.04 |    B     |    38.69   |   -90.40   |   32786  |
  | 10001 | 40.81 | -73.04 |    C     |    42.64   |   -73.78   |   84637  |
  | 10029 | 21.59 | -70.25 |    A     |    40.78   |   -74.44   |   75000  |
  | 10029 | 21.59 | -70.25 |    B     |    38.69   |   -90.40   |   75319  |
  | 10029 | 21.59 | -70.25 |    C     |    42.64   |   -73.78   |   87961  |
  | 20033 | 30.21 | -52.11 |    A     |    40.78   |   -74.44   |   23810  |
  | 20033 | 30.21 | -52.11 |    B     |    38.69   |   -90.40   |   56791  |
  | 20033 | 30.21 | -52.11 |    C     |    42.64   |   -73.78   |   52000  |
  | 30005 | 50.33 | -75.89 |    A     |    40.78   |   -74.44   |   63811  |
  | 30005 | 50.33 | -75.89 |    B     |    38.69   |   -90.40   |   96731  |
  | 30005 | 50.33 | -75.89 |    C     |    42.64   |   -73.78   |   75301  |
  | 10203 | 40.23 | -30.21 |    A     |    40.78   |   -74.44   |   10348  |
  | 10203 | 40.23 | -30.21 |    B     |    38.69   |   -90.40   |   89614  |
  | 10203 | 40.23 | -30.21 |    C     |    42.64   |   -73.78   |   59310  |
  </code></pre>
  <p>The zip table has about 45k rows, and the location table has about 25k rows. And below is my code, which runs extremely slow. I'm wondering if there're any ways to re-factor my code so that it runs faster?</p>
  <pre><code>from geopy.distance import geodesic
  mapped_data = []
  for index, i in zip.iterrows():
      centre = [i['lat'], i['lng']]
      location.loc[:, 'distance'] = location .apply(lambda x: geodesic((x[&quot;location_lat&quot;], x[&quot;location_lng&quot;]), centre).meters, axis=1)
      if len(output) != 0:
          output.loc[:, 'zip'] = i[zip]
          output.loc[:, 'lat'] = i['lat']
          output.loc[:, 'lng'] = i['lng']
      mapped_data.append(output)
  
  final_table = pd.concat(mapped_data)
  </code></pre>
  <p>Thanks!</p>
  <p>Edit:
  A cross join probably is faster, but, my tables are large. A cross join will get a 45k x 25k = 1B rows data set......</p>