Skip to content

Instantly share code, notes, and snippets.

@ShayanRiyaz
Created April 24, 2020 10:31
Show Gist options
  • Save ShayanRiyaz/155addac4e68379d4a57dbd3c5f8e992 to your computer and use it in GitHub Desktop.
Save ShayanRiyaz/155addac4e68379d4a57dbd3c5f8e992 to your computer and use it in GitHub Desktop.
def getNearbyVenues(names, latitudes, longitudes, radius=500):
venues_list=[]
for name, lat, lng in zip(names, latitudes, longitudes):
# create the API request URL
url = 'https://api.foursquare.com/v2/venues/explore?&client_id={}&client_secret={}&v={}&ll={},{}&radius={}&limit={}'.format(
CLIENT_ID,
CLIENT_SECRET,
VERSION,
lat,
lng,
radius,
LIMIT)
# make the GET request
results = requests.get(url).json()["response"]['groups'][0]['items']
# return only relevant information for each nearby venue
venues_list.append([(
name,
lat,
lng,
v['venue']['name'],
v['venue']['location']['lat'],
v['venue']['location']['lng'],
v['venue']['categories'][0]['name']) for v in results])
nearby_venues = pd.DataFrame([item for venue_list in venues_list for item in venue_list])
nearby_venues.columns = ['Neighbourhood',
'Neighbourhood Latitude',
'Neighbourhood Longitude',
'Venue',
'Venue Latitude',
'Venue Longitude',
'Venue Category']
return(nearby_venues)
la_venues = getNearbyVenues(names=nhoods['Neighbourhood'],
latitudes=nhoods['Latitude'],
longitudes=nhoods['Longitude']
)
print(la_venues.shape)
la_venues_count=la_venues.groupby('Neighbourhood').count()
la_venues_count.drop(la_venues_count.columns[[0,1,3,4,5]], axis=1,inplace=True)
la_venues_count.reset_index(inplace=True)
pop_neigh=la_venues_count[(la_venues_count.Venue>=10)]
pop_neigh.reset_index(drop=True,inplace=True)
pop_neigh
pop_list=pop_neigh['Neighbourhood'].values.tolist()
for i in range(0,len(la_venues)):
if la_venues.iloc[i,0] not in pop_list:
la_venues.iloc[i,0]='TO DROP'
la_venues=la_venues[la_venues.Neighbourhood!='TO DROP']
la_venues.reset_index(drop=True,inplace=True)
print('There are {} uniques categories.'.format(len(la_venues['Venue Category'].unique())))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment