Loop through rows in data frame of latitudes and longitudes and use reverse geo coder to find postcodes

29 views Asked by At

I am trying to loop through a dataframe of latitudes and longitudes to find the postcodes for each row, using geocoder (method: reverse), however, I get the error "TypeError: cannot convert the series to"

This is my code

import pandas as pd
df = pd.read_csv("Bunnings Co-Ordinates.csv")

import geocoder

for Store in df["Store"]:
    g = geocoder.google([df["Latitude"], df["Longitude"]], method='reverse', key="API_KEY")
    print(g.postal)

and I get the error "TypeError: cannot convert the series to".

I originally tried to loop through a list of co-ordinates using this code;

import geocoder
coordinates = [-27.59041, 153.02848, -34.93522, 138.53637]
latitudes = [-27.59041, -34.93522]
longitudes = [153.02848, 138.53637]

for coordinate in coordinates:
    g = geocoder.google([latitudes, longitudes], method='reverse', key="API_KEY")
    print(g.postal)

and I got this error: "TypeError: float() argument must be a string or a number, not 'list'"

1

There are 1 answers

0
Himanshu Panwar On

If you want to assign latitude and longitude the try this:

latitudes = [-27.59041, -34.93522]
longitudes = [153.02848, 138.53637]

for lat,long in zip(latitudes,longitudes):
    g = geocoder.google([lat, long], method='reverse', key="API_KEY")
    print(g.postal)