This file contains hidden or 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
import pandas as pd | |
education = pd.DataFrame({'Age': [5, 23, 16, 33, 78, 15, 41, 66]}) | |
bins = [0, 13, 19, 60, 100] | |
labels = ['Elementary', 'High School', 'Adult', 'Senior'] | |
education['Age Group'] = pd.cut(education['Age'], bins=bins, labels=labels) | |
print(education) | |
# Once the pd.cut() function has been applied, the original column will be replaced with a new column | |
# containing the bin labels. The bin labels are integers representing the bin that each value fell into. |
This file contains hidden or 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
#!/bin/bash | |
if [ $# -eq 0 ] | |
then | |
echo -e "You need to specify the target domain.\n" | |
echo -e "Usage:" | |
echo -e "\t$0 <domain>" | |
exit 1 | |
else | |
domain=$1 | |
fi |
This file contains hidden or 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
import pandas as pd | |
planets = pd.DataFrame({ | |
"Planet": ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn"], | |
"Distance_AU": [0.39, 0.72, 1, 1.52, 5.20, 9.58] | |
}) | |
dist = planets['Distance_AU'] | |
planets['dist_norm_z_score'] = (dist - dist.mean()) / dist.std() | |
# x_new = (x - x_min) / (x_max - x_min) | |
planets['dist_norm_min_max'] = (dist - dist.min()) / (dist.max() - dist.min()) | |
print(planets) |
This file contains hidden or 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
#!/bin/bash | |
if [ $# -eq 0 ] | |
then | |
echo -e "You need to specify the target domain.\n" | |
echo -e "Usage:" | |
echo -e "\t$0 <domain>" | |
exit 1 | |
else | |
domain=$1 | |
fi |
This file contains hidden or 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
import pandas as pd | |
df1 = pd.DataFrame(columns=['a', 'b', 'c', 'd', 'e']) | |
df1.loc[0] = pd.Series({'a': "α", 'b': "β", 'c': "γ", 'd': "δ", 'e': "ε"}) | |
df1.loc[1] = pd.Series({'a': "ζ", 'b': "η", 'c': "θ", 'd': "ι", 'e': "κ"}) | |
df1.loc[2] = pd.Series({'a': "λ", 'b': "μ", 'c': "ν", 'd': "ξ", 'e': "ο"}) | |
df1.loc[3] = pd.Series({'a': "π", 'b': "ρ", 'c': "σ", 'd': "τ", 'e': "υ"}) | |
df1.loc[4] = pd.Series({'a': "φ", 'b': "χ", 'c': "ψ", 'd': "ω", 'e': "Ω"}) | |
df2 = pd.DataFrame(columns=['a', 'b', 'c', 'd', 'e']) | |
df2.loc[0] = pd.Series({'a': "α", 'b': "β", 'c': "γ", 'd': "δ", 'e': "ε"}) | |
df2.loc[1] = pd.Series({'a': "ζ", 'b': "η", 'c': "charlie", 'd': "ι", 'e': "κ"}) |
This file contains hidden or 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
# Lead and Lag in SQL Server | |
SELECT | |
[SalesOrderID] | |
,[OrderDate] | |
,[CustomerID] | |
,[TotalDue] | |
,[NextTotalDue] = LEAD([TotalDue], 1) OVER(PARTITION BY [CustomerID] ORDER BY [SalesOrderID]) | |
,[PrevTotalDue] = LAG([TotalDue], 1) OVER(PARTITION BY [CustomerID] ORDER BY [SalesOrderID]) | |
FROM [AdventureWorks2019].[Sales].[SalesOrderHeader] | |
ORDER BY [SalesOrderID] |
This file contains hidden or 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
cursor = conn.cursor() | |
fetchall_cnt = 0 | |
for oven in all_ovens: | |
oven_id = oven[0] | |
sql_string = "SELECT * FROM product_orders WHERE product_id = %s" | |
cursor.execute(sql_string, [oven_id]) | |
oven_order = cursor.fetchall() | |
fetchall_cnt += 1 | |
print(oven_order, "/n") | |
print("fetchall_cnt:", fetchall_cnt) |
This file contains hidden or 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
# Generate Random Samples using Numpy Random Normal Distirubtion | |
μ, σ = 0, 0.1 | |
x = np.random.normal(μ, σ, 1000) | |
print(abs(μ - np.mean(x))) | |
print(abs(σ - np.std(x, ddof=1))) | |
import matplotlib.pyplot as plt | |
count, bins, ignored = plt.hist(x, 30, density=True) | |
plt.plot(bins, 1/(σ * np.sqrt(2 * np.pi)) * np.exp(-(bins - μ)**2 / (2 * σ**2)), linewidth=2, color='r') | |
plt.show() | |
print(np.random.normal(3, 2.5, size=(2, 4))) |
This file contains hidden or 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
-- Examples of JOIN ON in MS SQL Server | |
-- JOIN ON DATEDIFF | |
SELECT w2.[id] AS [ID] | |
FROM Weather AS w1 | |
JOIN Weather AS w2 | |
ON DATEDIFF(DAY, w1.[recordDate], w2.[recordDate]) = 1 | |
WHERE w2.[temperature] > w1.[temperature] | |
; | |
-- JOIN ON DATEADD | |
SELECT w1.[id] AS [ID] |
This file contains hidden or 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
# Interfacing with environment variables in Python | |
import os | |
os.environ["USERNAME"] = "new-user" | |
os.environ["PASSWORD"] = "new-password" | |
print(os.environ["USERNAME"]) | |
print(os.environ["PASSWORD"]) | |
print(os.environ["PATH"]) | |
# Retrieving the value of an environment variable with a default value | |
database_url = os.environ.get("DATABASE_URL", "localhost:5432") | |
print(database_url) |
NewerOlder