Jupyter Notebooks#

Throughout this textbook, we will be using Jupyter notebooks to work with data. While you can use pandas without Jupyter notebooks, using notebooks is highly encouraged as it helps you interactively view datasets and easily see the results of multiple small iterations of data processing. If you do not already have Jupyter notebooks installed, you can see installation instructions here.

Basic Layout#

The basic layout of a Jupyter notebook is fairly intuitive. You can open multiple cells and perform different tasks in them. Each cell can either be a code cell, a markdown cell (often used for writing text, like the cell containing this paragraph), or a raw cell (don’t worry about these for now). You can run each code cell, and you can also run the entire notebook in one go via the menu bar at the top. Feel free to explore the menu bar at the top to get a better idea of what all you can do!

Each time you open a Jupyter notebook, you are assigned a kernel, which is a computational engine that executes the code contained in a notebook document. Each kernel has a RAM limit, be careful you don’t exceed it! If you exceed it, your code will stop working. You can restart your kernel from the menu bar.

If you’re interested, you can find a detailed guide here.

Tips and Tricks#

The rest of this subchapter will be focused on tips and tricks you can use to make life easier when working with Jupyter notebooks.

Viewing Documentation#

You can use Jupyter to view the documentation of functions inside your notebook. The function must already be defined in the kernel (aka the cell defining the function must have already been run) for this to work.

Below, click your mouse anywhere on the print block below and use Shift + Tab to view the function’s documentation.

print('I hope you have a great day :)')
I hope you have a great day :)

Importing Libraries#

In Jupyter notebooks, we often work with functions from multiple libraries. It is a good practice to import any libraries you will be using at the top of the notebook. We have followed this practice throughout every chapter/subchapter in the course. Since we do not need any libraries for this subchapter, we have not imported anything.

Keyboard Shortcuts#

Even if you are familiar with Jupyter, we strongly encourage you to become proficient with keyboard shortcuts (this will save you time in the future). To learn about keyboard shortcuts, go to Help –> Keyboard Shortcuts in the menu above.

Here are a few that we like:

  1. Ctrl/Cmd + Return: Evaluate the current cell

  2. Shift + Return: Evaluate the current cell and move to the next

  3. Ctrl/Cmd + + or -: Zoom in or out

  4. Ctrl/Cmd + /: Comment or uncomment the selected code at once

  5. ESC : command mode (press before using any of the commands below)

    1. a : create a cell above

    2. b : create a cell below

    3. dd : delete a cell

    4. z : undo the last cell operation

    5. m : convert a cell to markdown

    6. y : convert a cell to code

Running Cells#

Aside from keyboard shortcuts (specifically Shift + Return), you can also run a single cell by clicking the Run button in the menu bar at the top of your notebook. If you hover over the button, you will also find some other options that allow you to run multiple cells. Specifically, restarting your kernel clears all saved variables and frees up memory. The Restart Kernel and Run upto Selected Cell option is particularly useful for situations where you believe your code is fine but you’re running into stange memory issues.