Can I optimize two for loops that look for the closest zip code based on lat/lon? I am new to python and I had the task to find the US zip code based on latitude and longitude. After messing with arcgis I realized that this was giving me empty values for certain locations. I ended up coding something that accomplishes my task by taking a dataset containing all US codes and using Euclidean distance to determine the closest zip code based on their lat/lon. However, this takes approximately 1.3 seconds on average to compute which for my nearly million records will take a while as a need a zip code for each entry. I looked that vectorization is a thing on python to speed up tasks. But, I cannot find a way to apply it to my code. Here is my code and any feedback would be appreciated:
for j in range(len(myFile)):
    p1=0
    p1=0
    point1 = np.array((myFile[&quot;Latitude&quot;][j], myFile[&quot;Longitude&quot;][j]))  # This is the reference point
    i = 0
    resultZip = str(usZips[&quot;Zip&quot;][0])
    dist = np.linalg.norm(point1 - np.array((float(usZips[&quot;Latitude&quot;][0]), float(usZips[&quot;Longitude&quot;][0]))))
    for i in range(0, len(usZips)):
        lat = float(usZips[&quot;Latitude&quot;][i])
        lon = float(usZips[&quot;Longitude&quot;][i])
        point2 = np.array((lat, lon))  # This will serve as the comparison from the dataset
        temp = np.linalg.norm(point1 - point2)
        if (temp &lt;= dist):  # IF the temp euclidean distance is lower than the alread set it will:
            dist = temp  # set the new distance to temp and...
            resultZip = str(usZips[&quot;Zip&quot;][i])  # will save the zip that has the same index as the new temp
            # p1=float(myFile[&quot;Latitude&quot;][58435])
            # p2=float(myFile[&quot;Longitude&quot;][58435])
        i += 1

I am aware Google also has a reverse geocoder API but it has a request limit per day.
The file called myFile is a csv file with the attributes userId, latitude, longitude, timestamp with about a million entries. The file usZips is public dataset with information about the city, lat, lon, zip and timezone with about 43k records of zips across the US.
So a better approach may be to :

covert your myFile to a Numpy array [[lat0, long0], [lat1, long10], ...].

lat_long = np.asarray(myFile)


Sort this array on latitude

lat_long = np.sort(lat_long, axis=0)


find the closest western points to the target point

closest_point_to_west = lat_long[lat_long[:, 0] &gt;= myPoint[0]]

Do the same for the other wind directions, this will give you the four closest points using Numpy's efficient slicing operations and you can do a distance measurement on only 4 of the points and not all of them.