Skip to content

Instantly share code, notes, and snippets.

@ShayanRiyaz
Created April 24, 2020 10:31
Show Gist options
  • Save ShayanRiyaz/c92b5b2f3c154691ecd84b0c738e227e to your computer and use it in GitHub Desktop.
Save ShayanRiyaz/c92b5b2f3c154691ecd84b0c738e227e to your computer and use it in GitHub Desktop.
def return_most_common_venues(row, num_top_venues):
row_categories = row.iloc[1:]
row_categories_sorted = row_categories.sort_values(ascending=False)
return row_categories_sorted.index.values[0:num_top_venues]
num_top_venues = 12
indicators = ['st', 'nd', 'rd']
# create columns according to number of top venues
columns = ['Neighbourhood']
for ind in np.arange(num_top_venues):
try:
columns.append('{}{} Most Common Venue'.format(ind+1, indicators[ind]))
except:
columns.append('{}th Most Common Venue'.format(ind+1))
# create a new data frame
Neighbourhoods_venues_sorted = pd.DataFrame(columns=columns)
Neighbourhoods_venues_sorted['Neighbourhood'] = la_grouped['Neighbourhood']
for ind in np.arange(la_grouped.shape[0]):
Neighbourhoods_venues_sorted.iloc[ind, 1:] = return_most_common_venues(la_grouped.iloc[ind, :], num_top_venues)
Neighbourhoods_venues_sorted.head()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment