How to create a loop for a search query using set coordinates from a data frame, while creating a new data frame for each search result?

Question:

I a really new to programming and can’t seem to find a way to create a loop that matches what I am looking for.

[dataframe with coordinates I want to loop]

conv_stores1 = yelp_api.search_query(categories= ‘convenience stores’, latitude=30.62779075491679, longitude=-96.33484825223623, limit=50)

this is the code I am trying to loop over.

conv_stores1 = yelp_api.search_query(categories= ‘convenience stores’, latitude=[0], longitude=[0], limit=50)

this is what I first attempted but it was not using the coordinates from my data frame so I wrote it manually. For the actaul loop I am not sure hopw to write it.

I apolagize if I am missing details or messed to in a super obvious way, thank you for your time and helping out.

Asked By: Marco Rous

||

Answers:

If I understand you correctly, to put it simply, you need something like the following;
Assuming your dataframe is called "df":

for i in range(df.shape[0]):
    conv_stores = yelp_api.search_query(categories= 'convenience stores', df['latitude'].iloc[i], df['longitude'].iloc[i], limit=50)
    print (conv_stores)

And a better version would be using a lambda function and adding the results as a new column to your original dataframe;

df['conv_stores'] = df.apply(lambda x: yelp_api.search_query(categories= 'convenience stores', x['latitude'], x['longitude'], limit=50), axis=1)
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.