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 / append_ws_v2.py
Created November 28, 2021 16:39
Add a worksheet to an Excel workbook (Version 2)
import pandas as pd
import openpyxl as pxl
from openpyxl.utils.dataframe import dataframe_to_rows
# Name of the workbook we'll be using
filename = 'test_wb.xlsx'
# For demo purposes, create a new 1-sheet workbook
# DataFrame to be inserted in the first worksheet
firstMockDF = pd.DataFrame({
@Ze1598
Ze1598 / append_ws.py
Last active January 25, 2024 02:15
Given an already existing Excel workbook, create and append new worksheets to the file using Pandas
import pandas as pd
import openpyxl as pxl
# DataFrame to be inserted in the first worksheet
firstMockData = {
'a': [1,2],
'b': [3,4]
}
firstMockDF = pd.DataFrame(firstMockData)
@Ze1598
Ze1598 / login-page.html
Last active April 12, 2023 22:37
Login page - HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="login-page.css">
<script defer src="login-page.js"></script>
</head>
@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_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 / login-page.css
Last active June 9, 2022 12:04
Login page - CSS
html {
height: 100%;
}
body {
height: 100%;
margin: 0;
font-family: Arial, Helvetica, sans-serif;
display: grid;
justify-items: center;
@Ze1598
Ze1598 / 1_download_data.py
Last active April 26, 2022 22:40
SP500 Forecast sourcing data
#from pandas_datareader import data as web
def download_data(tickers, start_date, end_date):
for ticker in tickers:
try:
print(f'Downloading {ticker}')
# Download data from Yahoo
temp_df = web.DataReader(ticker, 'yahoo', start_date, end_date)
# Create a column with only the ticker symbol
temp_df["Ticker"] = ticker
@Ze1598
Ze1598 / gems.py
Last active April 25, 2022 15:43
Falling gems animation with PyGame
import pygame
from random import randint, choice
# RGB colours
BLACK = (0, 0,0)
WHITE = (255, 255, 255)
GREEN = (44, 205, 23)
RED = (216, 10, 0)
BLUE = (27, 64, 255)
PURPLE = (121, 0, 234)
@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 = [