Best practices

Let's start with pep8 (https://www.python.org/dev/peps/pep-0008/)

Imports should be grouped in the following order:

  • standard library imports
  • related third party imports
  • local application/library specific imports

You should put a blank line between each group of imports. Put any relevant all specification after the imports.


In [ ]:
%matplotlib inline
%config InlineBackend.figure_format='retina' 

# Add this to python2 code to make life easier
from __future__ import absolute_import, division, print_function

from itertools import combinations
import string

from IPython.display import IFrame, HTML, YouTubeVideo
import matplotlib as mpl
from matplotlib import pyplot as plt
from matplotlib.pyplot import GridSpec
import seaborn as sns
import mpld3
import numpy as np
# don't do:
# from numpy import *

In [ ]:
import pandas as pd
import os, sys
import warnings

sns.set();
plt.rcParams['figure.figsize'] = (12, 8)
sns.set_style("darkgrid")
sns.set_context("poster", font_scale=1.3)

warnings.filterwarnings('ignore')

Look at Pandas Dataframes

this is italicized


In [ ]:
df = pd.read_csv("../data/coal_prod_cleaned.csv")

In [ ]:
# !conda install qgrid -y

In [ ]:
df.head()

In [ ]:
# Check out http://nbviewer.ipython.org/github/quantopian/qgrid/blob/master/qgrid_demo.ipynb for more (including demo)

In [ ]:
df.shape

In [ ]:
# This broke w/ Notebooks 5.0

In [ ]:
import qgrid # Put imports at the top
qgrid.nbinstall(overwrite=True)

In [ ]:
qgrid.show_grid(df[['MSHA_ID', 'Year', 'Mine_Name', 'Mine_State', 'Mine_County']], remote_js=True)

In [ ]:
ls

In [ ]:
# !conda install pivottablejs -y

In [ ]:
df = pd.read_csv("../data/mps.csv", encoding="ISO-8859-1")

In [ ]:
df.head(10)

Enhanced Pandas Dataframe Display


In [ ]:
# Province, Party, Average, Age, Heatmap

In [ ]:
from pivottablejs import pivot_ui

In [ ]:
pivot_ui(df)

Keyboard shortcuts

For help, ESC + h


In [ ]:
# in select mode, shift j/k (to select multiple cells at once)
# split cell with ctrl shift -

In [ ]:
first = 1

In [ ]:
second = 2

In [ ]:
third = 3

Different heading levels

With text and $\LaTeX$ support.


In [ ]:

You can also get monospaced fonts by indenting 4 spaces:

mkdir toc
cd toc

In [ ]:

Wrap with triple-backticks and language:

mkdir toc
cd toc
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh

In [ ]:


In [ ]:
import numpy as np

In [ ]:
np.linspace(start=, )

In [ ]:

SELECT *
FROM tablename

In [ ]:
```sql

SELECT first_name,
       last_name,
       year_of_birth
FROM presidents
WHERE year_of_birth > 1800;

```

In [ ]:
%%bash
pwd 
for i in *.ipynb
do
    du -h $i
done
echo "break"
echo
du -h *ipynb

Other cell-magics


In [ ]:
%%writefile ../scripts/temp.py
from __future__ import absolute_import, division, print_function

I'm not cheating!

In [ ]:
!cat ../scripts/temp.py

In [ ]:

Tab; shift-tab; shift-tab-tab; shift-tab-tab-tab-tab; and more!


In [ ]:
def silly_absolute_value_function(xval):
    """Takes a value and returns the value."""
    xval_sq = xval ** 2.0
    1 + 4
    xval_abs = np.sqrt(xval_sq)
    return xval_abs

In [ ]:
silly_absolute_value_function?

In [ ]:
silly_absolute_value_function??

In [ ]:
silly_absolute_value_function()

In [ ]:
import numpy as np

In [ ]:
# This doesn't work because ufunc 
np.linspace??

In [ ]:
# Indent/dedent/comment
for _ in range(5):
    df["one"] = 1
    df["two"] = 2
    df["three"] = 3
    df["four"] = 4

Multicursor magic


In [ ]:
df["one_better_name"] = 1
df["two_better_name"] = 2
df["three_better_name"] = 3
df["four_better_name"] = 4