Installation

By using pip,

pip install --upgrade pip # first, always upgrade pip!
pip install --upgrade ipython jupyter

By using conda,

conda install ipython jupyter

Or read more in this note.

Multiline commands

# Using '\'
df.columns = df.columns.str.replace('.', ' ')\
                       .str.replace('\s+', ' ')\
                       .str.strip().str.upper()

You CANNOT put # comments at the end of each line break!

Hotkeys / Shortcuts

There are 2 modes: command mode (pres ESC to activate) and edit mode (Enter to activate). Below are the most useful ones (for me).

You can edit / add more shortcuts in Help > Edit Keyboard Shortcuts.

For both modes,

  • Shift + Enter run the current cell, select below.
  • Ctrl + Enter run selected cells.
  • Alt + Enter run the current cell, insert below.
  • Ctrl + S save and checkpoint.

Command modes,

  • Enter take you into edit mode.
  • H show all shortcuts.
  • Up / Down select cell above / below.
  • Shift + Up / Down extend selected cells above / below.
  • A / B insert cell above / below.
  • X cut selected cells.
  • C copy selected cells.
  • V / Shift + V paste cells below / above.
  • D, D (press the key twice) delete selected cells.
  • Z undo cell deletion.
  • S Save and Checkpoint.
  • Y change the cell type to Code.
  • M change the cell type to Markdown.

Edit mode,

  • Esc take you into command mode.
  • Tab code completion or indent.
  • Ctrl + ] indent.
  • Ctrl + [ dedent.
  • Ctrl + A select all.
  • Ctrl + Z undo.
  • Ctrl + Shift + Z or Ctrl + Y redo.

Install new python package inside Jupyter Notebook

Using conda[ref],

# Install a conda package in the current Jupyter kernel
import sys
!conda install --yes --prefix {sys.prefix} numpy

# DON'T DO THIS
!conda install --yes numpy

Using pip,

# Install a pip package in the current Jupyter kernel
import sys
!{sys.executable} -m pip install numpy

# DON'T DO THIS
!pip install numpy

Display dataframes side-by-side

from IPython.display import display_html
def display_side_by_side(*args):
    html_str=''
    for df in args:
        html_str+=df.to_html()
    display_html(html_str.replace('table','table style="display:inline; margin-right: 5px;"'),raw=True)

display_side_by_side(df1,df2,df1)

Get previous outputs

_ # previous output
__ # second-to-last output
___ # third-to-last output

Display 2 figures side-by-side markdown cell

Put below codes in the markdown cell of Jupyter Notebook.

<tr>
  <td> <img src="Nordic_trails.jpg" alt="Drawing" style="width: 250px;"/> </td>
  <td> <img src="Nordic_trails.jpg" alt="Drawing" style="width: 250px;"/> </td>
</tr>

Check the info

Check the info of a function (gives us the documentation):

?<func-name>

Check the shortcodes of a function:

??<func-name>

Check where command executed from (in your $path)?

!type python
python is /Users/thi/anaconda/envs/python3.6/bin/python

Magic Functions

  • Check the full list (in examples) here or their docs here.
  • You can define your custom magic functions here.

Auto update the new updated modules (put at the beginning of the notebook)

%load_ext autoreload
%autoreload 2

Check more settings of %autoreload here.

Show the plots inside the notebook:

%matplotlib inline

Get the commands from 1 to 4:

%history -n 1-4 # get commands 1 to 4

Notice an error?

Everything on this site is published on Github. Just summit a suggested change or email me directly (don't forget to include the URL containing the bug), I will fix it.