Jupyter Notebook

How to use Jupyter Notebooks

What are Jupyter Notebook and JupyterLab?

Jupyter (Julia, Python, R) Notebook is the original interface of Project Jupyter and is an interactive environment that allows you to write and execute code, visualize data, and document your work all in one place. It supports various programming languages like Python, R, and Julia, but it’s most commonly used with Python. It is widely used for data science, machine learning, and research.

JupyterLab, on the other hand, is the next-generation interface of Jupyter. It builds upon the functionality of Jupyter Notebooks and provides a more flexible and powerful environment for working with code and data.

Here we show the basics of working with a Jupyter notebook to write text and code and save it in different formats. For detailed information on Jupyter Notebooks, refere to JupyterLab’s user guide.

Working with Jupyter Notebooks

Using Jupyter Notebooks is quite easy. You can write either code or text, including equations, in each cell of a notebook and execute code interactively and visualize and analyze data, variables and plot the data. The example below, including the text and code and code, has been written in a Jupyter Notebook.

First you open a Jupyter Notebook, which is automatically saved in your environment. The default path is your home directory. But if you change into a directory and then open a notebook, it is saved in the current directory. You can rename your notebook by right-clicking and choosing Rename.

Now we can start writing our notebook! We can start by writing a text, like the one you see in Step 1 in the example below. If you want to turn the cell into a text, you have to choose Markdown in the drop-down notebook menu at the top (Where you see Code) or with a shortcut key m.

step1

If you push Enter + Shift you execute the cell and now you see that you have a nice text!

step1

Then you can write a piece of code in the next cell and execute it.

step1

You can also drag and drop cells to rearrange your notebook or delete and easily add cells to your notebook.

When you are finished with your notebook, you can export it to other formats including Markdown, PDF, etc. You can do through File/Save and Export Notebook As ....

step1

Jupyter Notebooks are powered by kernels that execute the code you write. Each kernel consumes memory and computational resources, so it’s important to manage kernels. This helps keep your environment be fast and responsive. Please refer to Managing Kernels and Terminals page of this guide and JupyterLab’s User Guide for more information.

For practice, you can do the example below (in Python), work with data and analyse it in a Jupyter notebook.

Example: Analyzing and Visualizing Data in Python using a Jupyter Notebook

Let’s walk through a simple example where we load a dataset, perform basic analysis, and create a visualization, all within a Jupyter Notebook.

Step 1: Import Libraries

In this step, we import the necessary libraries to work with data and create visualizations.

import pandas as pd
import matplotlib.pyplot as plt

We use:

  • pandas to work with data.
  • matplotlib to create visualizations.

Step 2: Load a Dataset

Next, let’s load a sample dataset using pandas. For this example, we’ll use the well-known Iris dataset, which contains information about different types of flowers.

# Load Iris dataset
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"
data = pd.read_csv(url)
# Display the first five rows of the dataset
data.head()

sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa

The code above fetches the dataset from a public URL and displays the first five rows of data.

Step 3: Data Analysis

Let’s do some basic analysis of the dataset. For example, we can calculate the average petal length for each species of flower.

# Calculate average petal length by species
average_petal_length = data.groupby('species')['petal_length'].mean()
average_petal_length
species
setosa        1.462
versicolor    4.260
virginica     5.552
Name: petal_length, dtype: float64

Step 4: Data Visualization

Now, let’s create a simple bar plot to visualize the average petal length for each species.

# Create a bar plot for average petal length by species
average_petal_length.plot(kind='bar', color=['skyblue', 'lightgreen', 'salmon'])
plt.title('Average Petal Length by Species')
plt.ylabel('Petal Length (cm)')
plt.xlabel('Species')
plt.show()

png

Useful Links: