Customizations and Capabilities in IPython Notebook


Bhaskar S 01/09/2016


Introduction

In this article, we will explore few options to customize the IPython Notebook.

For this, we choose the open-source Anaconda Python 3 distribution, which includes all the necessary Python packages for science, math, & engineering computations as well as statistical data analysis.

Customizations in IPython Notebook

Open a terminal window and fire off the IPython Notebook.

To begin using DataFrame, one must import the pandas module as shown below:

Let us first import the numpy module as shown below:

import numpy as np

To generate 15 floating point numbers between the interval of numbers 1 and 10, invoke the linspace() method as shown below:

np.linspace(1,10, 15)

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.1

As can be seen from Fig.1 above, all the floating point numbers have a precision of 8 (8 digits after the decimal point). What if we desire all floating point numbers to have precision 3 (3 digits after the decimal point) ?

To format all floating point numbers from numpy to have precision 3 (3 digits after the decimal point), invoke the set_printoptions() method as shown below:

np.set_printoptions(precision=3)

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.2

Now, let us first import the pandas module as shown below:

import pandas as pd

Let us now create a DataFrame called df with 3 rows and 5 columns of random numbers as shown below:

df = pd.DataFrame(np.array([np.random.randn(5), np.random.randn(5), np.random.randn(5)]))

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.3

As can be seen from Fig.3 above, all the floating point numbers have a precision of 6 (6 digits after the decimal point). What if we desire all floating point numbers to have precision 3 (3 digits after the decimal point) ?

To format all floating point numbers from pandas to have precision 3 (3 digits after the decimal point), invoke the set_option() method as shown below:

pd.set_option('precision', 4)

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.4

As can be seen from Fig.3 and Fig.4 above, the default display of a DataFrame is an ugly looking table with rows and columns.

One can customize and make the DataFrame table display prettier by using an inline HTML-CSS style.

Enter the following HTML-CSS style in a IPython Notebook cell and execute it:

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.5

This will reformat all the DataFrame tables displayed in the current IPython Notebook.

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.6

Capabilities in IPython Notebook

One can display images (stored in files) inline in a IPython Notebook as shown below:

from IPython.display import display, Image

display(Image(filename='./images/custom-ipynb-07.jpg'))

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.7

One can embed and play an Youtube video inline in a IPython Notebook as shown below:

from IPython.display import YouTubeVideo

YouTubeVideo('xGbpuFNR1ME')

The following shows the screenshot of the result in IPython:

IPython Notebook
Fig.8

References

Introduction to IPython Notebook

Exploring NumPy

Exploring Pandas Series

Exploring Pandas DataFrame