Created
April 24, 2020 10:31
-
-
Save ShayanRiyaz/c92b5b2f3c154691ecd84b0c738e227e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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