Skip to content

Instantly share code, notes, and snippets.

View janbenetka's full-sized avatar

Jan Benetka janbenetka

  • Unacast
  • Pilsen, Czech Republic
View GitHub Profile
@janbenetka
janbenetka / colors_css_named.py
Created August 11, 2020 14:18
[Named colors] Default colors that have name in CSS. #python #color #css
colors = ['aliceblue', 'antiquewhite', 'aqua', 'aquamarine', 'azure',
'beige', 'bisque', 'black', 'blanchedalmond', 'blue',
'blueviolet', 'brown', 'burlywood', 'cadetblue',
'chartreuse', 'chocolate', 'coral', 'cornflowerblue',
'cornsilk', 'crimson', 'cyan', 'darkblue', 'darkcyan',
'darkgoldenrod', 'darkgray', 'darkgrey', 'darkgreen',
'darkkhaki', 'darkmagenta', 'darkolivegreen', 'darkorange',
'darkorchid', 'darkred', 'darksalmon', 'darkseagreen',
'darkslateblue', 'darkslategray', 'darkslategrey',
'darkturquoise', 'darkviolet', 'deeppink', 'deepskyblue',
@janbenetka
janbenetka / pandas_isin_in_query.py
Created August 13, 2020 22:51
[Query with list comparison ala .isin()] #pandas #query
df.query('a in ["a", "b", "c"]')
id_list = ["a", "b", "c"]
df.query('a in @id_list')
@janbenetka
janbenetka / leading_zero.sql
Created August 23, 2020 17:31
[Add leading zeros in BigQuery] #leading_zero #bigquery
SUBSTR(FORMAT("00%d", state_fips ), -2) as state_fips,
SUBSTR(FORMAT("00000%d", fips ), -5) as fips,
@janbenetka
janbenetka / bq_array_to_string
Created September 23, 2020 14:58
BigQuery - unpacking array to string #bigquery
ARRAY_TO_STRING(homeContext.fipscode, ',')
@janbenetka
janbenetka / cartoframes_plot_polygons.py
Last active September 23, 2020 21:22
[Cartoframes plot ploygons with points] #carto #python
from cartoframes.viz import Map, Layer, basemaps
from cartoframes.viz import Layer, color_continuous_style, color_bins_style, basic_style, color_category_legend
_df = average_duration.copy()
metric = 'avg_duration_day'
origin_county = selected_area_geom.iloc[0]['county_name']
origin_state = selected_area_geom.iloc[0]['state_name']
@janbenetka
janbenetka / airflow_slack_messaging.py
Created September 24, 2020 12:15
[Airflow > Slack message] Creation of DAG that notifies Slack channel #Airflow
slack_message = "Home & Work for bundleId {} is ready for shipping!".format("{{ ti.xcom_pull(task_ids='get_bundle_id') }}")
notify_slack = SimpleHttpOperator(
task_id="notify_slack",
endpoint=slack_webhook_url,
data=json.dumps(
{"attachments": [{"fallback": slack_message, "text": slack_message, "color": "good"}]}),
headers={"Content-Type": "application/json"},
depends_on_past=False,
response_check=lambda response: True if response.status_code == 200 else False,
http_conn_id="http_slack_hook"
@janbenetka
janbenetka / unacast_template.py
Created September 30, 2020 22:02
[Plotly Unacast Template] #plotly
!pip install plotly==4.8
# Quick way to install hind font
!npm install -g google-font-installer
!gfi install hind -v 300
import plotly.graph_objs as go
import plotly.io as pio
pd.options.plotting.backend = "plotly"
@janbenetka
janbenetka / partitioned_table.sql
Last active October 1, 2020 17:18
[BigQuery Partitioned Table DDL] #sql #bigquery
CREATE TABLE dataset.new_table
PARTITION BY DATE(timestamp_column) AS
SELECT x, y, z, timestamp_column
FROM dataset.existing_table
CREATE OR REPLACE TABLE `uc-prox-core-dev.14_days_retention.home_od_flux_2020_aggregated_condensed`
PARTITION BY minProcessingDate
AS
SELECT...
@janbenetka
janbenetka / pandas_display_options.py
Created October 3, 2020 22:23
[Pandas settings/options] Setting display options in pandas #pandas #dataframes
import pandas as pd
pd.options.display.max_columns = 50 # None -> No Restrictions
pd.options.display.max_rows = 200 # None -> Be careful with this
pd.options.display.max_colwidth = 100
pd.options.display.precision = 3
@janbenetka
janbenetka / total_row_col_pandas.py
Created October 3, 2020 22:25
[Total sum row and column in Pandas] Add total row and coumn in Pandas dataframe #pandas #dataframes
df = pd.DataFrame(dict(A=[2,6,3],
B=[2,2,6],
C=[3,2,3]))
df['col_total'] = df.apply(lambda x: x.sum(), axis=1)
df.loc['row_total'] = df.apply(lambda x: x.sum())