15 Tips and Tricks to Use Jupyter Notebook More Efficiently | by Julia Kho | Towards Data Science

In the data science community, Jupyter notebook is a popular tool with strong adoption. This article is meant to share cool tips and tricks to help you become more efficient while utilizing Jupyter notebook. Learn how to execute terminal commands from Jupyter, speed up your notebook by hiding outputs, add additional functionality to your notebook, and more!
Knowing these shortcuts can help you save time. I’ve highlighted in yellow the ones that I commonly used and find to be very helpful. If you forget the shortcut, you can always go to Command Mode and press H to see the full list. Jupyter notebook also allows you to create your own shortcut if you want. Click on the Edit Shortcut button to design your own hot keys.
Jupyter notebook only shows one output at a time as shown below. In the example, only the last variable’s output is shown.
However, you can add this code below to show all outputs in the cell. Notice now that both variables are shown.
If you wanted to insert an image, you will have to first change the cell type from Code to Markdown. You can do this in the dropdown box at the top of the page, or go to Command Mode and press M. Once the cell is a Markdown, simply just drag and drop the picture into the cell.
Once you’ve dropped the image into the cell, some code should appear. Run the cell (Shift + Enter) to see the image.
Rather than leaving Jupyter notebook to execute shell commands, you can use the exclamation point (!) at the beginning of your command. For example, you can install a package.
!pip install matplotlib-venn
Magic commands are special commands that help with productivity.
You might be most familiar with this magic command below, which lets your plots render in the notebook itself.
%matplotlib inline
Here are some other useful magic commands:
%pwd #print the current working directory%cd #change working directory%ls #show contents in the current directory%load [insert Python filename here] #load code into the Jupyter notebook%store [insert variable here] #this lets you pass variables between Jupyter Notebooks%who #use this to list all variables
For %who, you can specify the variable type as well. For example, the code below will list variables of all int type.
%who int
For the full list of magic commands:
%lsmagic #show all magic commands
Use %%time to get the wall time for your whole cell.
Let’s say you have multiple lines of code like below and you want to delete all the numbers in each line of the code. Rather than deleting each digit line by line, you can do it all at once!
Hold down the Alt key and select the entire cell content. Press the left arrow and you will see that there are now multiple cursors (black line in snippet below), one on each line. From here, you can then delete all the digits in one click of the delete key. Use the right arrow key if you want to move the cursor to the end.
If you have code that will take a while to run, you can add code to have Python tell you when it has finished running.
import winsoundduration = 1000  #millisecondsfreq = 440  #Hzwinsound.Beep(freq, duration)
This will sound an alarm when the code has completed.
import osos.system('say "your program has finished"')
Source: https://stackoverflow.com/questions/16573051/sound-alarm-when-code-finishes/16573339#16573339
Jupyter notebook extensions are neat tools to provide you with even more functionality.
Below is a list of configurable extensions you can enable. Some of the useful ones for me are collapsible headings, codefolding, scratchpad and spellchecker.
I recommend checking out the extensions and finding what’s useful for your work.
For help installing Jupyter Notebook Extensions to your computer, check out this link. https://jupyter-contrib-nbextensions.readthedocs.io/en/latest/install.html#
If you forget the parameters for a particular method, use Shift + Tab to get the documentation for that method. Click on the + arrow on the top right to expand the documentation.
There are a limited number of rows and columns that are shown in a pandas table, but you can customize the limit to your liking.
Here, I’ve set the max output of rows and columns to 500.
import pandas as pdpd.set_option('display.max_rows', 500)pd.set_option('display.max_columns', 500)
To make the important pieces of your outputs stand out, you can add bold font and/or color. I haven’t tried this one yet, but I thought it was a really cool trick provided by Stas Bekman. See snippet below.
Do you find your Jupyter Notebook lagging? That might possibly be because you have a lot of graphs that are being rendered.
Hover on the area to the left of your chart (see red rectangle below) and double click on the area to hide the output. This should speed up your notebook tremendously!
When you create a plot, you might get see this text "<matplotlib.collections.PathCollection at 0x9fae910>" right above the chart (highlighted yellow below).
I personally find that annoying, so to hide that line of text, you can add a semicolon to the end of the code.
With the markdown cells, it is easy to document your work. You can create a font hierarchy to organize your notebook by using the ‘#’ symbol as shown below.
Once the above is executed, this is what the font hierarchy looks like.
If you create these different headings and combine it with the collapsible headings Extension mentioned in Tip #9, it’s super useful to hide a large chunk of cells as well as being able to quickly navigate and move sections around.
Thanks for reading! If you have any favorite tip and tricks not listed in the article, I’d love to hear them in the comments.
Matplotlib Guide For People In A Hurry
Pandas for People In A Hurry
Numpy Guide for People In a Hurry
If you would like to download the Jupyter Notebook, you can find it here.