Skip to content

Instantly share code, notes, and snippets.

View toyeiei's full-sized avatar
🎯
Focusing

Kasidis Satangmongkol toyeiei

🎯
Focusing
View GitHub Profile
@toyeiei
toyeiei / .sql
Created November 9, 2018 01:11
SQL for Beginner - COUNT DISTINCT
-- count only distinct country in table customers
SELECT COUNT(DISTINCT country)
FROM customers;
@toyeiei
toyeiei / .sql
Created November 9, 2018 01:29
SQL for Beginner - Aggregate Function
-- Find key statistics for column total in table invoices
SELECT
COUNT(total),
AVG(total),
SUM(total),
MIN(total),
MAX(total)
FROM invoices;
-- use AS to rename columns
@toyeiei
toyeiei / .sql
Last active November 9, 2018 02:34
SQL for Beginner - GROUP BY P1
-- count number of customers by country
SELECT
country,
COUNT(*) AS n
FROM customers
GROUP BY country;
@toyeiei
toyeiei / .sql
Last active November 9, 2018 02:59
SQL for Beginner - Solutions Group By
-- QUIZ 1
-- count number of songs by genre
SELECT
genres.name,
COUNT(*) AS n
FROM genres
JOIN tracks
ON genres.genreid = tracks.genreid
GROUP BY genres.name;
@toyeiei
toyeiei / .py
Created November 11, 2018 00:01
Python tutorial - count items in a list, return a dict
# input
animals = ['dog', 'cat', 'cat', 'dog', 'dog', 'dog', 'cat', 'dog', 'hippo']
# expected output
# result = {'dog':5, 'cat':3, 'hippo':1}
# create empty dict to save our output
result = {}
# we write for loop and if-else
@toyeiei
toyeiei / .py
Last active November 11, 2018 00:08
Python tutorial - write simple function to count item in a list
# write a reusable function
def count_item(input_list):
"""count item in a list, return a dict"""
result = {}
for item in input_list:
if item in result:
result[item] += 1
else:
result[item] = 1
return result
@toyeiei
toyeiei / .py
Created November 11, 2018 00:15
test count_item() function with new lists
# input
animals = ['dog', 'cat', 'cat', 'dog', 'dog', 'dog', 'cat', 'dog', 'hippo']
genders = ['M', 'F', 'F', 'F', 'M']
balls = ['red', 'red', 'blue', 'blue', 'blue', 'black']
# test function
print(count_item(animals))
print(count_item(genders))
print(count_item(balls))
@toyeiei
toyeiei / .py
Last active November 11, 2018 05:38
Case Study - US Births Analysis Function
# write function for this analysis
def calculate_stat(data, k, v):
"""
input: data, idx key, idx value
output: dictionary with key: value pair
"""
result = {}
for row in data:
key = row.split(",")[k]
value = int(row.split(",")[v])
@toyeiei
toyeiei / .py
Last active November 11, 2018 06:30
Case Study - US Births Analysis
# cleaing data
data = data.split("\n")
data = data[1:]
# see what data looks like
for row in data[:10]:
print(row)
# preliminary analysis
result = {}
@toyeiei
toyeiei / .py
Created November 12, 2018 02:56
US birth test function
# test function
calculate_stat(data, 0, 4) # sum births by year
calculate_stat(data, 1, 4) # sum births by month
calculate_stat(data, 2, 4) # sum births by dom
calculate_stat(data, 3, 4) # sum births by dow