title: For loop is taking too long
body: <p>Edit: Here is the code above the loop for more helpful answers if it wasn't the problem.</p>
  
  <pre><code>import os
  import pandas as pd 
  import numpy 
  import csv 
  from math import *
  ParcelSize = 50
  UARFCN = 3087 
  
  y= r"C:\Users\Heba R\Desktop\GP\Pilot1.csv"
  x= r"C:\Users\Heba R\Desktop\GP\Cell.csv"
  
  scanner_File = pd.read_csv(y) 
  Cell_file = pd.read_csv(x)
  Cells = Cell_file[['Cell', 'Lat', 'Lon', 'SC', 'UARFCN', 'ANT_DIRECTION']]
  
  scanner = scanner_File[
          ['Latitude', 'Longitude', 'PSC: Top #1 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #1 (UARFCN #01)',
           'Sc Aggr Ec/Io (dB): Top #1 (UARFCN #01)',
           'PSC: Top #2 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #2 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #2 (UARFCN #01)',
           'PSC: Top #3 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #3 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #3 (UARFCN #01)',
           'PSC: Top #4 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #4 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #4 (UARFCN #01)',
           'PSC: Top #5 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #5 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #5 (UARFCN #01)',
           'PSC: Top #6 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #6 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #6 (UARFCN #01)',
           'PSC: Top #7 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #7 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #7 (UARFCN #01)',
           'PSC: Top #8 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #8 (UARFCN #01)', 'Sc Aggr Ec/Io (dB): Top #8 (UARFCN #01)',
           'PSC: Top #9 (UARFCN #01)', 'Sc Aggr Ec (dBm): Top #9 (UARFCN #01)',
           'Sc Aggr Ec/Io (dB): Top #9 (UARFCN #01)']]
  scanner_size = scanner.shape[0] 
  cells_size = Cells.shape[0]
  
  def CalcDistanceM(lat1, lon1, lat2, lon2): 
          lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2]) #convert decimal to rad 
          #haversine formula to calculate two points great circle distance on earth
          dlon = lon2 - lon1
          dlat = lat2 - lat1
          a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
          c = 2 * atan2(sqrt(a), sqrt(1 - a))
          distance = 6371 * c * 1000 #radius of earth in km =6371
          return distance
  
  def fn_CalcParcelID(Pos, ParcelUnitSize):
          if (Pos == 500):  # null parcel
              Result = int(50000000)
          elif (Pos &lt; 0):
              Result = int(Pos * 100000) - ParcelUnitSize + (int(Pos * 100000) % ParcelUnitSize)
          else:
              Result = int(Pos * 100000) - (int(Pos * 100000) % ParcelUnitSize)
          return int(Result)
  
  A1=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A2=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A3=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A4=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A5=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A6=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A7=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A8=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  A9=pd.DataFrame(columns=['Latitude','Longitude','PSC','EcNo','RSCP'])
  for i in range (scanner_size):
         #if isnan(scanner['PSC: Top #1 (UARFCN #01)'][i]) == False:
         if (scanner['PSC: Top #1 (UARFCN #01)'][i]) != -1 :
            A1 = A1.append({ 'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                              'PSC': scanner['PSC: Top #1 (UARFCN #01)'][i],'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #1 (UARFCN #01)'][i],'RSCP': scanner['Sc Aggr Ec (dBm): Top #1 (UARFCN #01)'][i]}, ignore_index=True)
  
         if (scanner['PSC: Top #2 (UARFCN #01)'][i]) !=-1:
             A2 = A2.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #2 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #2 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #2 (UARFCN #01)'][i]}, ignore_index=True)
         if (scanner['PSC: Top #3 (UARFCN #01)'][i]) != -1:
             A3 = A3.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #3 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #3 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #3 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #4 (UARFCN #01)'][i]) != -1:
             A4 = A4.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #4 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #4 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #4 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #5 (UARFCN #01)'][i]) != -1:
             A5 = A5.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #5 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #5 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #5 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #6 (UARFCN #01)'][i]) != -1:
             A6 = A6.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #6 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #6 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #6 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #7 (UARFCN #01)'][i]) != -1:
             A7 = A7.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #7 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #7 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #7 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #8 (UARFCN #01)'][i]) != -1:
             A8 = A8.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #8 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #8 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #8 (UARFCN #01)'][i]}, ignore_index=True)
         if  (scanner['PSC: Top #9 (UARFCN #01)'][i]) != -1:
             A9 = A9.append({'Latitude': scanner['Latitude'][i], 'Longitude': scanner['Longitude'][i],
                             'PSC': scanner['PSC: Top #9 (UARFCN #01)'][i],
                             'EcNo': scanner['Sc Aggr Ec/Io (dB): Top #9 (UARFCN #01)'][i],
                             'RSCP': scanner['Sc Aggr Ec (dBm): Top #9 (UARFCN #01)'][i]}, ignore_index=True)
  A=pd.concat([A1,A2,A3,A4,A5,A6,A7,A8,A9],sort=False)
  A = A[~A[['Latitude','Longitude','PSC','EcNo','RSCP']].apply(frozenset, axis=1).duplicated()] #~ is bitwise not frozenset elem remain unchanged after creation
  A.to_csv('table_data_pilot.csv',index=True)
  A = pd.read_csv('table_data_pilot.csv')
  #A=A.iloc[:50,:].reset_index()
  A_size = A.shape[0]
  
  for i in range(A_size):
        j = i +1
        for j in range (A_size):
           dLat=A['Latitude'][i] - A['Latitude'][j]
           dLon=A['Longitude'][i] - A['Longitude'][j]
           if abs(dLat) &lt; 0.00045 and abs(dLon) &lt; 0.00045:
             distance = CalcDistanceM(A['Latitude'][j], A['Longitude'][j],
                                                  A['Latitude'][i],
                                                  A['Longitude'][i])
             print (distance)
  
  B1 = pd.DataFrame(columns=['Lat','Lon','UARFCN','PSC','SC_Avg_EcNo','SC_Avg_RSCP'])
  </code></pre>
  
  <p>First of all, I just got started in Python and therefore I do not have much knowledge. I tried to search for similar problems and I could not find a proper solution.I am using the following code:</p>
  
  <pre><code>for i in range(A_size):
        x1=float(fn_CalcParcelID(A['Latitude'][i], ParcelSize) )/ 100000
        x2=float(fn_CalcParcelID(A['Longitude'][i], ParcelSize) ) / 100000
        B1 = B1.append({'Lat': x1, 'Lon': x2,
                              'PSC': A ['PSC'][i],
                              'UARFCN':UARFCN,
                              'SC_Avg_EcNo':A['EcNo'][i],
                              'SC_Avg_RSCP': A['RSCP'][i]
  
                              }, ignore_index=True)
  B1.to_csv('B1.csv')
  </code></pre>
  
  <p>The loop is meant to calculate new latitude and longitude and then making a new csv file.
  A is a csv file that has almost 23000 rows and 42 column </p>