Skip to content

Instantly share code, notes, and snippets.

@ShayanRiyaz
Created April 24, 2020 10:30
Show Gist options
  • Save ShayanRiyaz/01ce2112298e44aaba6d8692eaf0d1c8 to your computer and use it in GitHub Desktop.
Save ShayanRiyaz/01ce2112298e44aaba6d8692eaf0d1c8 to your computer and use it in GitHub Desktop.
neighbourhood_latitude = nhoods.loc[0, 'Latitude'] # neighbourhood latitude value
neighbourhood_longitude = nhoods.loc[0, 'Longitude'] # neighbourhood longitude value
neighbourhood_name = nhoods.loc[0, 'Neighbourhood'] # neighbourhood name
print('Latitude and longitude values of {} are {}, {}.'.format(neighbourhood_name,
neighbourhood_latitude,
neighbourhood_longitude))
LIMIT = 100 # limit of number of venues returned by Foursquare API
radius = 500 # define radius
# create URL
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
neighbourhood_latitude,
neighbourhood_longitude,
radius,
LIMIT)
results = requests.get(url).json()
# function that extracts the category of the venue
def get_category_type(row):
try:
categories_list = row['categories']
except:
categories_list = row['venue.categories']
if len(categories_list) == 0:
return None
else:
return categories_list[0]['name']
venues = results['response']['groups'][0]['items']
nearby_venues = json_normalize(venues) # flatten JSON
# filter columns
filtered_columns = ['venue.name', 'venue.categories', 'venue.location.lat', 'venue.location.lng']
nearby_venues =nearby_venues.loc[:, filtered_columns]
# filter the category for each row
nearby_venues['venue.categories'] = nearby_venues.apply(get_category_type, axis=1)
# clean columns
nearby_venues.columns = [col.split(".")[-1] for col in nearby_venues.columns]
nearby_venues
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment