Skip to content

Instantly share code, notes, and snippets.

View holman57's full-sized avatar
🦐
how shrimple could it be?

Luke Holman holman57

🦐
how shrimple could it be?
View GitHub Profile
@holman57
holman57 / bins.py
Last active June 20, 2024 00:44
The pd.cut() function takes a number of arguments, including the column to be discretized, the number of bins, and the bin boundaries. The bin boundaries can be specified as a list of values, or as a pandas Interval Index object.
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.
@holman57
holman57 / arguments.sh
Created June 10, 2024 17:51
Check arguments passed to a bash shell
#!/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
@holman57
holman57 / min_max_normalization.py
Created June 8, 2024 21:26
Normalize planet distances using Min-Max normalization
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)
@holman57
holman57 / CIDR.sh
Last active June 7, 2024 21:02
Check for given arguments, identify network range for the specified IP address(es), ping discovered IP address(es), identify IP address(es) of the specified domain; and lastly, list available options.
#!/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
@holman57
holman57 / dataframe_difference.py
Last active June 4, 2024 03:43
Select only the rows from DataFrame df1 that are different from, or are not present in, Dataframe df2
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': "κ"})
@holman57
holman57 / Lead_Lag.sql
Created June 2, 2024 02:31
Lead and Lag in SQL Server, grab the previous or next value in a field
# 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]
@holman57
holman57 / cursor.py
Last active May 26, 2024 22:00
Cursor manipulation in Python
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)
@holman57
holman57 / numpy_random_normal.py
Created May 25, 2024 19:01
Generate Random Samples using Numpy Random Normal Distirubtion
# 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)))
@holman57
holman57 / JOIN_ON.sql
Last active May 19, 2024 20:06
Examples of JOIN ON in MS SQL Server T-SQL
-- 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]
@holman57
holman57 / Environment_Variables.py
Last active May 12, 2024 05:38
Interfacing with environment variables in user space with Python
# 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)