I am working on Pandas and I am struggling to create a new column with information from an API over the rows of my dataframe.
The "location" column I want to iterate is a Series of dictionaries like this:
{'type': 'Point', 'coordinates': [-0.1394759, 51.5170385]}
My function:
def starbucks(df):
API_key = os.getenv('API_KEY')
lat = list(df["location"])[1]["coordinates"][1]
lon = list(df["location"])[1]["coordinates"][0]
base_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
endpoint = "query=starbucks&location={0},{1}&radius=1000&key={2}".format(lat, lon, API_key)
res = requests.get(base_url+endpoint).json()
How I am implementing the apply function (currently with a 'location', 'occurred at index 61'
error):
sample['testing'] = sample.apply(lambda x: starbucks(x["location"]), axis=1)
I've read some posts regarding the apply function and the documentation, but I am still missing something.
Help would be very much appreciated!
Thanks