Reputation: 89
I want to retrieve geo data (long/lat) from Mapzen for addresses in Germany. Mapzen provides an API which asks for a key. Each request returns a Json.
This following code returns the long/lat and address name for one address:
import pandas as pd
import request
BASE_URL = 'https://search.mapzen.com/v1/search'
txt = 'Stübekamp 33, Hamburg, Germany'
resp = requests.get(BASE_URL, params = {'api_key': "YourKey", 'size': 1, 'text': txt})
data = resp.json()
Full = pd.DataFrame(columns=["Long", "Lat", "Street"])
LongLat = data["bbox"][0:2]
Street = data["features"][0]["properties"]["label"]
Full.loc[1] = pd.Series({"Long": LongLat[1], "Lat": LongLat[0], "Street": Street})
I tried to replace the txt argument to loop over it, but as long as I understand the request.get method cannot be looped over. Therefore, I followed this approach and defined a function which I use in a for loop.
What I want the for loop to do is to paste the string of one row in addresses in the txt argument in the function. This should be done n times, whereas n is the length of the addresses vector. The retrieved information (long/lat/address) should be added to a new row in the AllAddresses DataFrame. So in the end I have a DataFrame with three Columns ("Long", "Lat", "Street") and in this case 3 rows.
def Getall(Input):
resp = requests.get('https://search.mapzen.com/v1/search', params = {'api_key': "YourKey", 'size': 1, 'text': Input})
data = resp.json()
LongLat = data["bbox"][0:2]
Street = data["features"][0]["properties"]["label"]
Full = pd.DataFrame(columns=["Long", "Lat", "Street"])
Full.loc[1] = pd.Series({"Long": LongLat[1], "Lat": LongLat[0], "Street": Street})
return Full
addresses = pd.DataFrame(["Stübekamp 33, Hamburg, Germany", "Mesterfeld 28, Hamburg, Germany","Beutnerring 2, Hamburg, Germany"])
AllAddresses = []
for index, row in addresses.iterrows():
Input = row("0")
data = Getall(Input)
AllAddresses.append = data
This code however, returns the error:
TypeError: 'Series' object is not callable
I read that iterrows is the way to go, but I am coming from R and feel a little lost here.
Upvotes: 0
Views: 208
Reputation: 2831
Addresses is a pandas dataframe for no apparent reason. Then you iterate over it which is generally a bad idea if you needed a pandas dataframe which you don't. Then you take "row" which is a series and call it like a function row("0"). As it is not a function you get an error. Just make addresses a list to solve your first problem.
Then of course you will find you have a problem with Full which also does not need to be a dataframe; you cannot add a row like that; and you are returning a dataframe for each row which is likely not what you want either.
Upvotes: 1