terraform unter Linux installieren.

Geht in euren Projektordner und legt einen tmp Ordner an und wechselt hinein.

project> mkdir tmp
project> cd temp
project/tmp>

Holt euch unter https://www.terraform.io/downloads.html für eure Destribution die passende URL. Für mich ist das unter Linux -> 64bit und dann via Rechtsklick und “Copy link addess” hole ich mir die URL. Führt damit jetzt:

project/tmp> wget <URL>

aus. Entpackt die heruntergeladene Zip-Datei mit:

project/tmp> unzip <terrform_download.zip>

Damit wurde jetzt ein Binary “terraform” entpackt. Kopiert das Binary jetzt mit:

project/tmp> sudo cp terraform /usr/local/bin/

Nach /usr/local/bin. Von dort ist es nun von überall ausführbar, da /usr/local/bin in eurer PATH Umgebungsvariablen stehen sollte. Das könnt ihr prüfen durch folgende Eingabe:

project/tmp> echo $PATH

In der Ausgabe solltest ihr jetzt auch die Pfadangabe /usr/local/bin  finden.

Prüft die Erreichbarkeit von terraform kurz durch:

project/tmp> cd ..
project> terraform -h

Passt alles könnt ihr den tmp Ordner jetzt löschen…

project> rm -rf tmp

… und dann dann mit terraform loslegen.

Viel Spaß!

Zwei Spalten in Pandas addieren und dabei nans beachten via lamdba-Funktion mit Bedigung

Ihr wollte in eurem Dataframe zwei Spalten addieren und dabei nans ignorieren? D.h. ihr wollt a und b addieren, sofern a und b nicht None oder null sind und a, wenn a nicht None oder null aber b None oder null ist und sonst b in die neue Spalte schreiben?

Das geht so:

add_if_notna = lambda a, b: a+b if ~np.isnan(a) and ~np.isnan(b) else a if ~np.isnan(a) else b
df['new_col'] = df['col1'].combine(df['col2'],add_if_notna)

Wenn ihr alternativ die Spalten nur addieren wollt, wenn beide Werte ungleich null / None sind, dann geht das so:

add_if_notna = lambda a, b: a+b if ~np.isnan(a) and ~np.isnan(b) else None
df['new_col'] = df['col1'].combine(df['col2'],add_if_notna)

 

Wie man aus einer PostgreSQL Datenbank direkt in einen Pandas Dataframe selektiert.

Unter der Annahme, dass ihr host, port, db_name, user_name und passwort definiert habt, könnt ihr dazu folgendes Snippet verwenden.

import pandas as pd
import pandas.io.sql as sqlio
import psycopg2

conn = psycopg2.connect(f"host='{host}' port={port} dbname='{db_name}' user={user_name} password={password}")
query = "select count(*) from table;"
df = sqlio.read_sql_query(query, conn)
conn.close()