Skip to content

Instantly share code, notes, and snippets.

@c-l-nguyen
c-l-nguyen / employee_dept_icicle.csv
Created June 6, 2021 19:32
Data for icicle chart
ID Label Level Level ID Value
1 A HR 1 20
1 B IT 1 30
1 C Sales 1 15
1 D Finance 1 35
2 A1 HR 2 10
3 A2 HR 2 5
4 A3 HR 2 5
2 B1 IT 2 15
3 B2 IT 2 15
@c-l-nguyen
c-l-nguyen / init.lua
Last active February 28, 2021 10:55
Hammerspoon virtual numpad for macOS
k = hs.hotkey.modal.new('ctrl-shift', 'n')
function k:entered() hs.alert'Virtual Numpad' end
function k:exited() hs.alert'Exit Virtual Numpad' end
k:bind('ctrl-shift', 'n', function() k:exit() end)
hs.fnutils.each({
{ key='j', padkey='pad1'},
{ key='k', padkey='pad2'},
{ key='l', padkey='pad3'},
{ key='u', padkey='pad4'},
@c-l-nguyen
c-l-nguyen / data-melt.py
Last active January 17, 2021 17:16
Melt pandas DataFrame from wide, fat format to long, skinny format
import pandas as pd
df = pd.read_csv("Global Temperature Anomalies.csv")
print(df.columns)
# parameter values
id_vars = ['Hemisphere', 'Year']
cols_to_pivot = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
'Aug', 'Sep', 'Oct', 'Nov', 'Dec', 'J-D', 'D-N', 'DJF', 'MAM', 'JJA',
'SON']
# append all DataFrames into one before writing to single table in Hyper
final_df = df1.append(df2).append(df3).reset_index(drop=True)
pantab.frames_to_hyper({"pokemon": final_df}, "hyper/all_pokemon_in_one.hyper")
# this produces the same result as above
pantab.frame_to_hyper(df1, "hyper/all_pokemon_append.hyper", table = "pokemon")
pantab.frame_to_hyper(df2, "hyper/all_pokemon_append.hyper", table = "pokemon", table_mode = "a")
pantab.frame_to_hyper(df3, "hyper/all_pokemon_append.hyper", table = "pokemon", table_mode = "a")
print(pantab.frames_from_hyper("hyper/pokemon_public.hyper").keys())
# dict_keys([TableName('public', 'pokemon')])
print(pantab.frames_from_hyper("hyper/pokemon_extract.hyper").keys())
# dict_keys([TableName('Extract', 'pokemon')])
# this errors out with message "HyperException: Specified table does not exist: pokemon."
pantab.frame_from_hyper("hyper/pokemon_extract.hyper", table = "pokemon")
# need to specify schema to read table from
pantab.frame_from_hyper("hyper/pokemon_extract.hyper", table = TableName("Extract", "pokemon"))
# these two are equivalent
pantab.frame_from_hyper("hyper/pokemon_public.hyper", table = "pokemon")
pantab.frame_from_hyper("hyper/pokemon_public.hyper", table = TableName("public", "pokemon"))
import pandas as pd
import pantab
from tableauhyperapi import TableName
df = pd.read_csv("data/starter_pokemon_grass.csv")
# save DataFrame output to .hyper file
pantab.frame_to_hyper(df, "hyper/pokemon_public.hyper", table = "pokemon")
# save to schema other than public
df1 = pd.read_csv("data/starter_pokemon_grass.csv")
df2 = pd.read_csv("data/starter_pokemon_fire.csv")
df3 = pd.read_csv("data/starter_pokemon_water.csv")
dict_df = {"grass": df1, "fire": df2, "water": df3}
pantab.frames_to_hyper(dict_df, "hyper/all_pokemon_separate.hyper")
import csv
from tableauhyperapi import HyperProcess, Telemetry, Connection, CreateMode, NOT_NULLABLE, NULLABLE, SqlType, \
TableDefinition, Inserter, escape_name, escape_string_literal, HyperException, TableName
with HyperProcess(telemetry=Telemetry.DO_NOT_SEND_USAGE_DATA_TO_TABLEAU) as hyper:
print("starting up Hyper Process")
with Connection(hyper.endpoint, 'hyper/pokemon_hyper_api.hyper', CreateMode.CREATE_AND_REPLACE) as connection:
print("creating or replacing pokemon_hyper_api.hyper")
poke_table = TableDefinition(TableName('public','pokemon'), [