Jupyter Notebooks in Depth

Authors
Dr. Sangeetha Nandakumar | Dr. Nicholas Del Grosso

Setup

Import Libraries

import pandas as pd
import hvplot.pandas

Download Data

import os
import owncloud

if not os.path.exists('data'):
    print('Creating directory for data')
    os.mkdir('data')

if not os.path.exists('data/steinmetz_winter2017.csv'):
    oc = owncloud.Client.from_public_link('https://uni-bonn.sciebo.de/s/G5rdvTsoESXolF4')
    oc.get_file('/', 'data/steinmetz_winter2017.csv')

Section 1: Jupyter Notebooks To Make Advanced Analysis Documents

Jupyter Notebooks are a powerful tool to combine your code and add analysis documentation all in the same document. In this session, we start by understanding different types of basic unit of a Jupyter notebook known as cell starting with Code cell. Then we look into some of ways to integrate text based explanations into analysis document with Markdown and Raw cells. Now that we are familiar with the structure of Jupyter notebook, we move on to looking at the data itself within a notebook using Pandas. Finally, we use hvPlot to add visualizations.

To make things easier, you can use the following shortcut table to quickly navigate and edit Jupyter notebook.

Category Shortcut Action
Cell Operations Shift + Enter Run the current cell and move to the next
Ctrl + Enter Run the current cell but stay on the same cell
Ctrl + Shift + - Split the current cell at the cursor
Cell Insertion and Deletion Esc + A Insert a new cell above
Esc + B Insert a new cell below
Esc + D + D Delete the current cell
Esc + Z Undo cell deletion
Cell Type Conversion Esc + M Convert the current cell to Markdown
Esc + Y Convert the current cell to Code
Esc + R Convert the current cell to Raw
Editing and Saving Ctrl + S Save the notebook
Ctrl + / Toggle comment on the selected code line(s)

Section 2: Code Cell

Cell is a basic unit of Jupyter notebook. It is where we write, execute, and organize our code and notes. There are three types of cells in Jupyter: Code, Markdown, and Raw cells.

A code cell is to write and execute code. It allows us to enter code in programming languages like Python, R, or Julia and execute it directly within the notebook. When executed, the code cell displays the output below it, such as printed results, visualizations, or even errors.

Code Description
12 + 11 Adds two numbers together.
var = 100 Assigns the value 100 to the variable var.
var = 12 + 11 Adds 12 and 11 and assigns the result to var.
print("hello") Prints the string "hello" to the console.
df = pd.read_csv(file) Reads a CSV file into a Pandas DataFrame.
df[column].hvplot.hist() Plots a histogram of the given column.
df[column].hvplot.box() Plots a box plot of the given column.

Arithmetic expressions are computed and displayed directly in the output.

Exercises

Example: In a code cell, type 1+1 and execute the cell (Ctrl+Enter). What do you see?

1 + 1
2

Exercise: Type “Hello” and execute the cell.

Solution
"Hello"
'Hello'

Exercise: Type 10 and execute the cell.

Solution
10
10

Assigining a value to a variable does not produce an output. It just stores the variable.

Example: Example Assign 10 to a

a = 10

Exercise: Assign “hello” to greet

Solution
greet = "hello"

Exercise: Assign c to sum of 10 and 100.

Solution
c = 10 + 100

If you want to display the value of a variable, you need to reference the.

Example: Assign 10 to a and display a.

a = 10
a
10

Exercise: Assign “hello” to greet and display it.

Solution
greet = "hello"
greet
'hello'

Exercise: Assign c to sum of 10 and 100 and display c

Solution
c = 10+100
c
110

Print statements are widely used in almost all languages for debugging, displaying outputs, tracking program flow, etc. Let’s understand the behaviour of a code cell for print statements.

Example: print “hello”.

print("hello")
hello

Exercise: print “hello” and assign it to a variable called print_result. What do you see?

Solution
print_result = print("hello")
hello

Exercise: print “hello” and assign it to a variable called print_result and display print_result. What do you see? How is it different from the above output?

Solution
print_result = print('hello')
print_result
hello

Essentially, printing a variable does not return anything as it’s sole purpose is to print something directly on the screen. So print_result does not hold any value.

Exercise: print “hello” and assign it to a variable called print_result and print print_result. What do you see?

Solution
print_result = print('hello')
print(print_result)
hello
None

If you make a mistake in your code (such as dividing by zero or using an undefined variable), the code cell will return an error message:

Example: Divide 5 by 0.

5 / 0

Exercise: Add 10 to “hello”

Solution
10 + "hello"

Exercise: print variable_does_not_exist

Solution
print(variable_does_not_exist)

Plots will appear right below the code. For the below exercises, do not worry about understanding the code. It is to understand the capabilities of notebooks.

Example: Plot histogram of ‘response_time’ variable from Winter 2017 data.

df = pd.read_csv('data/steinmetz_winter2017.csv')
df['response_time'].hvplot.hist()

Exercise: Plot histogram of ‘feedback_time’ variable from Winter 2017 data.

Solution
df = pd.read_csv('data/steinmetz_winter2017.csv')
df['feedback_time'].hvplot.hist()

Exercise: Plot box plot of ‘response_time’ variable from Winter 2017 data.

Solution
df = pd.read_csv('data/steinmetz_winter2017.csv')
df['response_time'].hvplot.box()

Section 3: Markdown Cell

Markdown cells render formatted text for documentation, using Markdown syntax. Markdown in Jupyter Notebooks is a great tool for formatting text, embedding images, and organizing information.

We can create headings using # symbols. More number of # preceding the heading text, smaller is the font size.

Exercises

Example Create a markdown cell that looks like this:

This is a level 1 heading

Solution:

# This is a level 1 heading

Exercise: Create a level 3 heading that looks like this

This is a level 3 heading

Solution
### This is a level 3 heading

Exercise: Create a level 6 heading that looks like this

This is a level 6 heading
Solution
###### This is a level 6 heading

You can also make the text bold, italics, or both.

Example Make text bold

Solution:

**This is bold**

Exercise: Write “This is italics” in italics in the cell below by using only one * instead of two on either side of the text.

Solution
*This is italics*

Exercise: Write “This is bold and italics” in both bold and italics in the cell below by using *** on either side of the text

Solution
***This is bold and italics***

Example: Make an unordered list of three programming languages using -.

Solution:

- Python
- Julia
- R

You can also use *, or + to create unordered lists.

Exercise: Make an unordered list of your three favorite fruits using *

Solution
* Mango
* Watermelon
* Orange

Exercise: Make an unordered list of your favorite vegetables using +.

Solution
+ Bell peppers
+ Cauliflower
+ Brocolli¨

Exercise: Order your favorite vegetables starting from most favorite using 1 to 3 to number your list.

Solution
1. Cauliflower
2. Brocolli
3. Bell peppers

Demo

Create a link to a website, an equation, a python code snippet, and a table in markdown.

Link to website

[iBOTS Website](https://ibehave.nrw/ibots-platform/about-ibots/)

will produce

iBOTS Website

Equation

$E=mc^2$

will produce:

E=mc2E=mc^2

Python code snippet

```python
print("Hello, world!")

will produce

print("Hello, world!")

Table

| Name | Age |
|---|---|
| John  | 20  |
| Jane  | 25  |
| Jill  | 30  |

will produce

Name Age
John 20
Jane 25
Jill 30

Section 4: Raw Cell

Raw cells do not execute code or render as Markdown. Instead, they are left untouched during execution, allowing us to store plain text or any other content exactly as it is entered.

Exercises

Example Type “hello” in a raw cell. (Esc + R turns a cell into raw cell)

hello

Exercise: Type 10 + 10 in a raw cell

Solution
10 + 10

Exercise: Type ##### Is this a header? in a raw cell

Solution
##### Is this a header?

Exercise: What happens when you type the following into markdown cells?

  • Hello
  • 10+10
  • ##### Is this a header?
Solution

10 + 10

Hello

Is this a header?