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 [1]:
%matplotlib inline
%config InlineBackend.figure_format='retina'
# Add this to python2 code to make life easier
from __future__ import absolute_import, division, print_function
import numpy as np
# don't do:
# from numpy import *
In [2]:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import ipywidgets
import os
import 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')
In [3]:
df = pd.read_csv("../data/coal_prod_cleaned.csv")
In [4]:
df.head()
Out[4]:
In [5]:
df.shape
Out[5]:
In [8]:
# import qgrid # Put imports at the top
# qgrid.nbinstall(overwrite=True)
# qgrid.show_grid(df[['MSHA_ID',
# 'Year',
# 'Mine_Name',
# 'Mine_State',
# 'Mine_County']], remote_js=True)
# Check out http://nbviewer.ipython.org/github/quantopian/qgrid/blob/master/qgrid_demo.ipynb for more (including demo)
http://nicolas.kruchten.com/content/2015/09/jupyter_pivottablejs/
In [16]:
!conda install pivottablejs -y
In [9]:
df = pd.read_csv("../data/mps.csv", encoding="ISO-8859-1")
In [10]:
df.head(10)
Out[10]:
In [11]:
# Province, Party, Average, Age, Heatmap
In [12]:
from pivottablejs import pivot_ui
In [13]:
pivot_ui(df)
Out[13]:
In [14]:
import numpy as np
In [14]:
np.random.
In [ ]:
np.linspace(start=, )
In [ ]:
np.linspace(50, 150, num=100,)
In [ ]:
np.linspace(start=, )
In [15]:
np.linspace?
In [16]:
np.linspace??
In [17]:
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 [19]:
silly_absolute_value_function(2)
Out[19]:
In [20]:
silly_absolute_value_function?
In [20]:
silly_absolute_value_function??
In [ ]:
In [ ]:
In [ ]:
# in select mode, shift j/k (to select multiple cells at once)
# split cell with ctrl shift -
In [21]:
first = 1
In [21]:
second = 2
In [21]:
third = 3
In [ ]:
first = 1
In [ ]:
second = 2
In [ ]:
third = 3
In [71]:
# a new cell above
# b new cell below
In [21]:
%%latex
If you want to get crazier...
\begin{equation}
\oint_S {E_n dA = \frac{1}{{\varepsilon _0 }}} Q_\textrm{inside}
\end{equation}
In [24]:
# Indent
# Cmd + [
# Cmd + ]
# Comment
# Cmd + /
You can also get monospaced
fonts by indenting 4 spaces:
mkdir toc
cd toc
Wrap with triple-backticks and language:
mkdir toc
cd toc
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh
SELECT *
FROM tablename
In [22]:
# note difference w/ lab
SELECT first_name,
last_name,
year_of_birth
FROM presidents
WHERE year_of_birth > 1800;
In [23]:
%%bash
pwd
for i in *.ipynb
do
echo ${i} | awk -F . '{print $1}'
done
echo
echo "break"
echo
for i in *.ipynb
do
echo $i | awk -F - '{print $2}'
done
In [78]:
%%writefile ../scripts/temp.py
from __future__ import absolute_import, division, print_function
I promise that I'm not cheating!
In [79]:
!cat ../scripts/temp.py
https://gist.github.com/jbwhit/38c1035c48cdb1714fc8d47fa163bfae
In [24]:
%load_ext autoreload
%autoreload 2
In [ ]:
In [25]:
example_dict = {}
In [26]:
# Indent/dedent/comment
for _ in range(5):
example_dict["one"] = 1
example_dict["two"] = 2
example_dict["three"] = 3
example_dict["four"] = 4
In [87]:
example_dict["one_better_name"] = 1
example_dict["two_better_name"] = 2
example_dict["three_better_name"] = 3
example_dict["four_better_name"] = 4
Find and replace -- regex notebook (or cell) wide.
In [27]:
import numpy as np
In [24]:
!conda install -c r rpy2 -y
In [28]:
import rpy2
In [29]:
%load_ext rpy2.ipython
In [91]:
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
In [30]:
%%R?
In [93]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
In [94]:
type(XYcoef)
Out[94]:
In [95]:
XYcoef**2
Out[95]:
In [34]:
thing()
Out[34]: