Snippets Collections
Notebook Best Practices

Use Collapsible Headings and Table of Content
Notebooks should be executable from top to bottom
Name your variables carefully
Use dummy names such as tmp or _ when needed
Clear useless variables when not needed (del my_variable)
Clear your code and merge cells when relevant (Shift-M)
Hide your cell outputs to gain space (double-click on the red Out[]: section to the left of your cell).
from IPython.display import HTML, IFrame
IFrame("http://www.youtube.com/embed/8QiPFmIMxFc?t=388", width="560", height="315")
from ipywidgets import interact

@interact
def plot_polynom(a=[0,1,2,3], b=2):
    x = np.arange(-10, 10, 0.1)
    y = a*x**3+ b*x**2    
    plt.plot(x,y); plt.xlim(xmin=-10, xmax=10); plt.ylim(ymin=-100, ymax=100)
from ipywidgets import interact

@interact
def plot_polynom(a=[0,1,2,3], b=2):
    x = np.arange(-10, 10, 0.1)
    y = a*x**3+ b*x**2    
    plt.plot(x,y); plt.xlim(xmin=-10, xmax=10); plt.ylim(ymin=-100, ymax=100)
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns

# Set style to 'seaborn' - high resolution graphs
plt.style.use('seaborn')
%config InlineBackend.figure_format = 'retina'

df = pd.read_csv('path/to/file.csv')
df.head()

df.info()
cat_type = CategoricalDtype(categories=['3', '2', '1'], ordered=True)
cat_type2 = CategoricalDtype(categories=['Kind','Jong','Middelbaar','Oud'], ordered=True)
​
df1['pclass'] = df1['pclass'].map({1: '1', 2: '2', 3: '3'})
df1['survived'] = df1['survived'].map({1: True, 0: False})
df1['sex'] = df1['sex'].map({'male': 'M', 'female': 'V'})
df1['pclass'] = df1['pclass'].astype(cat_type)
df1['sex'] = df1['sex'].astype('category')
df1['age'] = df1['age'].map(lambda x: round(x))
df1['age'] = df1['age'].astype('int8')
df1['fare'] = df1['fare'].map(lambda x: round(x, 2))
df1['age_cat'] = pd.cut(df1['age'], bins=4, labels=('Kind','Jong','Middelbaar','Oud'))
df1['age_cat'] = df1['age_cat'].astype(cat_type2)
df1 = df1.filter(items=['pclass', 'name', 'survived', 'sex', 'age', 'age_cat', 'fare'])
df1
<div style="background-color:rgb(250,250,250); padding:30px;box-shadow:0 1px 1px rgba(0, 0, 0, 0.2);">
    <p style="font-family:Helvetica;font-size:15px;font-weight:bold">
       Text....
    </p>
</div>
<div style="background-color:#1abc9c; padding:1px 20px 20px 20px; border-radius:5px;box-shadow: rgba(0, 0, 0, 0.25) 0px 0.0625em 0.0625em, rgba(0, 0, 0, 0.25) 0px 0.125em 0.5em, rgba(255, 255, 255, 0.1) 0px 0px 0px 1px inset;">
    <h1 style="font-family: 'Lato', sans-serif;color:white;padding-left:10px;font-size:50px">
        Heading
    </h1>
</div>

<div class="alert alert-block alert-info" style="border-radius:8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 3px 0px, rgba(0, 0, 0, 0.06) 0px 1px 2px 0px;">
    <b>&#9432; Note<br /></b> Use blue boxes (alert-info) for tips and notes.
    If it’s a note, you don’t have to include the word “Note”.
</div>
<div class="alert alert-block alert-warning" style="border-radius:8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 3px 0px, rgba(0, 0, 0, 0.06) 0px 1px 2px 0px;">
    <b>!&#x20DD; Important<br /></b> Use yellow boxes if you to underline important things.
</div>
<div class="alert alert-block alert-danger" style="border-radius:8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 3px 0px, rgba(0, 0, 0, 0.06) 0px 1px 2px 0px;">
    <b>&#9888; Warning<br /></b> In general, avoid the red boxes. These should only be
    used for actions that might cause data loss or another major issue.
</div>
<div class="alert alert-block alert-success" style="border-radius:8px; box-shadow: rgba(0, 0, 0, 0.1) 0px 1px 3px 0px, rgba(0, 0, 0, 0.06) 0px 1px 2px 0px;">
    <b>&#10149; See also<br /></b> Use green boxes to link to other documentation sources.
</div>
conda create -n my-conda-env                               # creates new virtual env
conda activate my-conda-env                                # activate environment in terminal
conda install ipykernel                                    # install Python kernel in new conda env
ipython kernel install --user --name=my-conda-env-kernel   # configure Jupyter to use Python kernel
jupyter notebook                                           # run jupyter from system
(firstEnv)
>>conda install -c anaconda ipykernel
>>python -m ipykernel install --user --name=firstEnv
#!/usr/bin/env python3

from multiprocessing import Pool

def run(task):
  # Do something with task here
    print("Handling {}".format(task))

if __name__ == "__main__":
  tasks = ['task1', 'task2', 'task3']
  # Create a pool of specific number of CPUs
  p = Pool(len(tasks))
  # Start each task within the pool
  p.map(run, tasks)
# this line will write the code below into a Python script called script.py
%%writefile script.py
star

Sat Nov 26 2022 10:37:00 GMT+0000 (UTC)

#jupyter
star

Sat Nov 26 2022 10:31:55 GMT+0000 (UTC)

#jupyter
star

Wed Aug 10 2022 05:32:42 GMT+0000 (UTC) http://localhost:8888/notebooks/titanic.ipynb

#jupyter #notebook #titanic #dataset
star

Thu Dec 09 2021 09:47:17 GMT+0000 (UTC)

#jupyter #markdown
star

Thu Dec 09 2021 09:46:17 GMT+0000 (UTC)

#jupyter #markdown
star

Wed Nov 10 2021 10:32:25 GMT+0000 (UTC)

#html #jupyter #markdown
star

Wed Nov 10 2021 10:32:00 GMT+0000 (UTC)

#html #jupyter #markdown
star

Wed Nov 10 2021 10:31:18 GMT+0000 (UTC)

#html #jupyter #markdown
star

Wed Nov 10 2021 10:30:02 GMT+0000 (UTC)

#html #jupyter #markdown
star

Wed May 12 2021 00:33:13 GMT+0000 (UTC)

#jupyter #anaconda
star

Fri Apr 02 2021 06:41:25 GMT+0000 (UTC)

#python #jupyter

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension