Skip to content

Instantly share code, notes, and snippets.

View Ze1598's full-sized avatar

José Fernando Costa Ze1598

  • Porto, Portugal
View GitHub Profile
@Ze1598
Ze1598 / custom_sort_order.py
Created November 1, 2020 21:45
Create a custom sort for a pandas DataFrame column: months example
import pandas as pd
import numpy as np
def generate_random_dates(num_dates: int) -> np.array:
"""Generate a 1D array of `num_dates` random dates.
"""
start_date = "2020-01-01"
# Generate all days for 2020
available_dates = [np.datetime64(start_date) + days for days in range(365)]
# Get `num_dates` random dates from 2020
@Ze1598
Ze1598 / fcc_gbc_complete.py
Last active October 21, 2020 19:27
freeCodeCamp grouped bar chart visualization: complete script
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the CSV (load date data as proper date types)
df = pd.read_csv("page_views.csv")
df["date"] = pd.to_datetime(df["date"])
# Sort the DF from oldest to most recent recordings
df.sort_values(by="date", inplace=True)
# Use the column of dates as the DF's index
@Ze1598
Ze1598 / fcc_gbc_visualization.py
Last active March 13, 2023 19:02
freeCodeCamp grouped bar chart visualization: data visualization
# Pivot the DF so that there's a column for each month, each row\
# represents a year, and the cells have the mean page views for the\
# respective year and month
df_pivot = pd.pivot_table(
df,
values="page_views",
index="year",
columns="month",
aggfunc=np.mean
)
@Ze1598
Ze1598 / fcc_gbc_preprocess.py
Last active March 13, 2023 19:02
freeCodeCamp grouped bar chart visualization: data pre-processing
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
# Load the CSV (load date data as proper date types)
df = pd.read_csv("page_views.csv")
df["date"] = pd.to_datetime(df["date"])
# Sort the DF from oldest to most recent recordings
df.sort_values(by="date", inplace=True)
# Use the column of dates as the DF's index
@Ze1598
Ze1598 / regex_expenses.txt
Created October 5, 2020 11:15
Extract expenses with regex from txt (sample data)
0.70 - Coffee
0.75 - Cake slice
19.99 - Video game
12 - Lunch
6.99 - Spotify monthly charge
22.24 - Travel mug
7 - Cinema ticket
15 - Lunch
Total: 85.62
@Ze1598
Ze1598 / regex_expenses.py
Last active October 5, 2020 11:14
Extract expenses with regex from txt
import re
import datetime
import pandas as pd
PATTERN = r'(^([\d]+)([\.]?)([\d]*))( - )(.*)'
# Load text
with open("expenses.txt", "r") as f:
expenses_txt = f.readlines()
# Put all the lines into a single string
@Ze1598
Ze1598 / update_dataframe.py
Last active September 9, 2020 18:04
Update a DataFrame based on common values of two DataFrames
import pandas as pd
import numpy as np
# Current employee dataset
original = pd.DataFrame({
"id": [1, 2, 3, 4],
"name": ["Michael", "Jim", "Pam", "Dwight"],
"age": [46, 35, 35, 38],
"email": ["[email protected]", "[email protected]", "[email protected]", "[email protected]"],
"in_company": [True, True, True, True]
@Ze1598
Ze1598 / calculate_text_size.py
Last active July 28, 2020 13:16
Calculate text size (Python, PIL)
def get_text_dimensions(text_string, font):
# https://stackoverflow.com/a/46220683/9263761
ascent, descent = font.getmetrics()
text_width = font.getmask(text_string).getbbox()[2]
text_height = font.getmask(text_string).getbbox()[3] + descent
return (text_width, text_height)
@Ze1598
Ze1598 / centered_text.py
Last active October 11, 2021 22:37
Create images with centered text (PIL)
from PIL import Image, ImageDraw, ImageFont
from textwrap import wrap
def get_y_and_heights(text_wrapped, dimensions, margin, font):
"""Get the first vertical coordinate at which to draw text and the height of each line of text"""
# https://stackoverflow.com/a/46220683/9263761
ascent, descent = font.getmetrics()
# Calculate the height needed to draw each line of text (including its bottom margin)
line_heights = [
@Ze1598
Ze1598 / db_extraction.py
Last active July 22, 2020 21:06
psycopg2 tutorial: data extraction
from configparser import ConfigParser
import psycopg2
import psycopg2.extras as psql_extras
import pandas as pd
from typing import Dict, List
def load_connection_info(
ini_filename: str
) -> Dict[str, str]: