Skip to content

Instantly share code, notes, and snippets.

View gbarreiro's full-sized avatar

Guillermo Barreiro gbarreiro

View GitHub Profile
@gbarreiro
gbarreiro / bash_cheatsheet_filter_output.sh
Last active December 6, 2020 18:23
Bash cheatsheet: filter output
cat verylongfile.txt | more # shows the content of verylongfile.txt split in pages
cat verylongfile.txt | less # shows the content of verylongfile.txt split in pages, with support for scrolling and backwards navigation
cat verylongfile.txt | grep "I love you" # shows the lines where the string "I love you" is found
@gbarreiro
gbarreiro / bash_cheatsheet_help.sh
Created December 6, 2020 18:19
Bash cheatsheet: help and man
help nano # brief summary about the command "nano"
man nano # detailed info about the command "nano"
@gbarreiro
gbarreiro / bestcuisines_insight_4.py
Created September 20, 2020 14:57
Best cuisines: insight 4
# Calculate the average percentage of restaurants of each cuisine for the 9 analyzed cities
famous_cuisines = cuisines_city_normalized.mean(axis=0)
# Get the top 10 cuisines around the world
famous_cuisines = famous_cuisines.sort_values(ascending=True)
famous_cuisines = famous_cuisines.tail(10)
# Plot the data in a horizontal bar graph
famous_cuisines.plot(kind='barh')
plt.xlabel('Average % of restaurants in the cities')
@gbarreiro
gbarreiro / bestcuisines_insight_3.py
Created September 20, 2020 14:54
Best cuisines: insight 3
percentages = {}
for city, cuisine in city_local_cuisines.items():
# For each city, get the percentage of local cuisine restaurants
percentage = cuisines_city_normalized.at[city, cuisine]
percentages[city] = percentage
# Plot the data in a bar graph
df_percentages = pd.Series(percentages).sort_values(ascending=True)
df_percentages.plot(kind='barh')
plt.xlabel('% of local cuisine restaurants')
@gbarreiro
gbarreiro / bestcuisines_insight_2.py
Created September 20, 2020 14:51
Best cuisines: insight 2
fig = plt.figure()
for index, cuisine in enumerate(city_local_cuisines.values()):
# For the selected cuisine, plot a bar graph with the cities where there are more restaurants of that cuisine
ax = fig.add_subplot(3, 3, index+1)
cuisine_cities = cuisines_city_normalized[cuisine]
cuisine_cities = cuisine_cities.sort_values(ascending=True).head(9)
# Plot in red the bar with the city where this cuisine is local, plot in blue the rest
color = ''
for i in cuisine_cities.keys():
@gbarreiro
gbarreiro / bestcuisines_insight_1.py
Created September 20, 2020 14:47
Best cuisines: insight 1
fig = plt.figure()
for index, city in enumerate(cities):
# For each city, get the top 10 cuisines and plot them in a horizontal bar graph
ax = fig.add_subplot(3, 3, index+1)
# Get the top 10 cuisines for the city
cuisines = cuisines_city_normalized.loc[city]
cuisines = cuisines.sort_values(ascending=False).head(10).sort_values(ascending=True)
# Plot in red the bar with the local cuisine, plot in blue the rest
# One-hot encoding
restaurants_onehot = pd.get_dummies(restaurants['Cuisine'], prefix="", prefix_sep="")
restaurants_onehot = restaurants_onehot.multiply(restaurants['Number of restaurants'], axis=0)
restaurants_onehot = pd.concat([restaurants['City'], restaurants_onehot], axis=1)
# Group by city
cuisines_city = restaurants_onehot.groupby('City').sum().reset_index()
cuisines_city = cuisines_city.set_index(keys='City')
# Normalize the numbers
@gbarreiro
gbarreiro / bestcuisines_get_restaurants.py
Last active September 22, 2020 09:31
Best cuisines: get the number of restaurants for each cuisine on each city
restaurants = pd.DataFrame(columns=['City', 'Cuisine', 'Number of restaurants']) # define the DataFrame where the results will be stored
for city in cities:
for cuisine_name, category_id in cuisines.items():
# For each city, retrieve the top restaurants from Foursquare for each cuisine...
query = requests.get("{url}&near={city}&categoryId={category_id}".format(url=URL, city=city, category_id=category_id))
if query.status_code == 200:
number = query.json()
restaurants = restaurants.append({
'City': city,
@gbarreiro
gbarreiro / bestcuisines_setup.py
Last active September 20, 2020 14:46
Best cuisines: setup code
# Define the dependencies
import pandas as pd
import requests
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
mpl.style.use(['seaborn'])
# Foursquare API credentials and endpoint
CLIENT_ID = # your client ID
@gbarreiro
gbarreiro / docker-compose.yml
Last active February 17, 2021 14:56
Docker Compose sample file
version: "3.8"
services:
database:
image: mysql:latest
volumes:
- db-data:/var/lib/mysql/data
command: --default-authentication-plugin=mysql_native_password # run this command in the container
networks:
- database-api
restart: always # the container must be always on