This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Hello | |
``` | |
This is an %>% amazing() %>% gist() | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
# initialize the "count"-variable to session state | |
if "count" not in st.session_state: | |
st.session_state["count"] = 0 | |
# clicked will be True when the button was clicked | |
# and False otherwise (for example if another action | |
# was performed or if it has not been clicked yet) | |
clicked = st.button("Click here!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# the most common way | |
my_object = { | |
"a": 5, | |
"b": 6 | |
} | |
# what I prefer (avoiding { and ") | |
my_object = dict(a=5, b=6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = { "a": 5, "b": 5 } | |
b = { "c": 5, "d": 5 } | |
c = { **a, **b } | |
assert c == { "a": 5, "b": 5, "c": 5, "d": 5 } | |
# order matters! | |
# the last added will overwrite the first added | |
c = { **a, **b, "a": 10 } | |
assert c == { "a": 10, "b": 5, "c": 5, "d": 5 } | |
b["a"] = 10 | |
c = { **a, **b } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
# list of dicts to pd.DataFrame | |
df = pd.DataFrame([ | |
{ "a": 5, "b": 6 }, | |
{ "a": 6, "b": 7 } | |
]) | |
# df = | |
# a b | |
# 0 5 6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# remove keys | |
a = dict(a=5, b=6, c=7, d=8) | |
remove = set(["c", "d"]) | |
a = { k: v for k,v in a.items() if k not in remove } | |
# a = { "a": 5, "b": 6 } | |
# keep keys | |
a = dict(a=5, b=6, c=7, d=8) | |
keep = remove | |
a = { k: v for k,v in a.items() if k in keep } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
a = dict(a=5, b=6) | |
# to json | |
json_string = json.dumps(a) | |
# json_string = '{"a": 5, "b": 6}' | |
# json to dict | |
assert a == json.loads(json_string) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import random | |
counter = Counter() | |
# add lists with random number of a, b and c. | |
counter.update([random.choice(["a", "b", "c"]) for _ in range(100)]) | |
counter.update([random.choice(["a", "b"]) for _ in range(100)]) | |
# merge with counts | |
counter.update({ "a": 10000, "b": 1 }) | |
# add directly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
# by default a dict | |
a = defaultdict(dict) | |
assert a[5] == {} | |
a[5]["a"] = 5 | |
assert a[5] == { "a": 5 } | |
# by default a list | |
a = defaultdict(list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
# Generate two series that are correlated | |
original_x = pd.Series(np.random.uniform(size=100)) | |
original_y = 1.3*original_x + np.random.normal(0, 0.1, size=100) | |
# Now create shifted versions, | |
# I create two examples, one where x is shifted |
OlderNewer