Skip to content

Instantly share code, notes, and snippets.

View aecorn's full-sized avatar

Carl Corneil aecorn

View GitHub Profile
@aecorn
aecorn / statbank_v2_beta.py
Created December 3, 2024 22:50
Statbank v2 beta
import requests
import pandas as pd
import itertools
def get_table_ids(shortname: str) -> list[str]:
shortname_content = requests.get("https://data.ssb.no/api/pxwebapi/v2-beta/navigation/{shortname}").json()
return [x for x in [table.get("id") for table in shortname_content.get("folderContents")] if x.isdigit() and len(x) == 5]
def table_dataframe(tableid: str) -> pd.DataFrame:
@aecorn
aecorn / secret.ts
Last active February 28, 2024 21:08
Quartz plugin for removing secret chunks from markdown
import { QuartzTransformerPlugin } from "../types"
const secret_regex = new RegExp(/(---secret[\s\S]*?secret---)/, "g")
export const SecretRemover: QuartzTransformerPlugin = () => {
return {
name: "SecretRemover",
textTransform(_ctx, src) {
src = src.toString()
src = src.replaceAll(secret_regex, (value, ...capture) => {
@aecorn
aecorn / proc_contents.py
Last active February 6, 2024 12:16
Cheap proc contents and head-rows from Pandas SAS7BDATReader
import pandas as pd
from pandas.io.sas.sas7bdat import SAS7BDATReader
def proc_contents(path: str) -> pd.DataFrame:
sasfile = SAS7BDATReader(path,
convert_dates=False,
blank_missing=False,
convert_text=False,
chunksize=1,
)
@aecorn
aecorn / repr_html_example.py
Created November 20, 2023 12:29
Alternative display in IPython to print and repr through _repr_html_ or _ipython_display_
import pandas as pd
class Wrapper:
def __init__(self, df: pd.DataFrame):
self.data = df
def __str__(self):
return str(self.data.info())
def __repr__(self):
@aecorn
aecorn / Convert SAS-label string to dict + json
Last active September 1, 2023 07:28
Pythoncode to take a label string from sas and convert to dict plus spit out a local json-file, in case you want to open it in a seperate notebook later.
import json
labels_script = """
fnr = 'Fødselsnummer'
snr_nudb = 'Snr'
kjoenn = 'kjønn (lages ut fra snr)'
fodt_aar = 'fødselsår yyyy (lages ut fra snr)'
fodt_dato = 'fødselsdato yyyymmdd (hentes fra befolkningsfila)'
fodato = 'Oppholdsdato første opphold i Norge yyyymmdd (hentes fra befolkningsfila)'
fodeland = 'Fødeland (hentes fra befolkningsfila)'
@aecorn
aecorn / SSB colors manipulation
Last active February 2, 2023 06:45
Some values from the design-manual of statistics norway, conversion from hex to rgba-values
colors = [
"#1a9d49",
"#3396d2",
"#9582bb",
"#f26539",
"#d2bc2a",
"#6f9090"]
alpha = 0.65
# Convert colors to rgba, and give them the set opacity
from copy import deepcopy
from collections import Counter
def restructure_belop(inp: dict) -> dict:
skjemaer_ny = deepcopy(skjemaer)
for skjema_gml, skjema_ny in zip(skjemaer, skjemaer_ny):
counts = Counter([elem["funksjon/kapittel"] for elem in skjema_gml["regnskap"]])
code_groups = [k for k, v in counts.items() if v > 1]
for code in code_groups:
elems = [x for x in skjema_gml["regnskap"] if x["funksjon/kapittel"] == code]
for elem in elems:
@aecorn
aecorn / Power BI m-script
Last active April 21, 2021 11:04
Latvian Stat-API
let
tableId = "NBA040",
PostContents = "{
""query"": [
{
""code"": ""AgeGroup"",
""selection"": {
""filter"": ""item"",
""values"": [
""Y15-74""
@aecorn
aecorn / Densityplot
Last active April 16, 2021 06:52
Python visualizations in Power BI
import matplotlib.pyplot as plt
import seaborn as sns
# Initialize seaborn object
d = sns.kdeplot(data = dataset, shade = True, shade_lowest = False, cmap = 'vlag')
# Set some attributes on the seaborn-object
d.set(ylim=(0,55), xlabel = 'Two first numbers of NACE', ylabel = 'Number of Employees per company')
# Display the visual
plt.show()
@aecorn
aecorn / Violinplot
Created April 16, 2021 06:43
Python visualizations in Power BI
import matplotlib.pyplot as plt
import seaborn as sns
# Limit data
dataset = dataset[dataset.NACE_first.isin([1, 2, 3])]
# Initialize seaborn object
v = sns.violinplot(data = dataset, x = 'NACE_first', y = 'employee_points', palette = 'Set3', inner = 'quart')
# Set some attributes on the seaborn-object
v.set(ylim=(0,120), xlabel = 'First Nace Number', ylabel = 'Number of Employees per company')
# Remove some of the borders