10 Tricks for Data Scientists using Jupyter Notebooks | by Benedikt Droste | Towards Data Science

When I started to use Python for data analysis, I found out very fast that Jupyter notebooks are a great tool to give my code a better structure. However, some things were still annoying. If I use headlines for better readability, larger notebooks get quickly confusing. If I calculate more than one value in a cell, I receive just an output for the last variable and so on and so on...But hey, we are using Python and other people had the same problems and found great solutions for it. I want to share some of them.
Table of contents:
1. Two datasets side by side2. Print every output from a specific cell3. Change screen width4. Jupyter extensions4.1 Code folding4.2 Collapsible heading4.3 Execution time4.4 Scratchpad4.5 Kernle idle4.6 Zenmode4.7 Table beautifier
Sometimes I want to compare two smaller dataframes side by side. With the following css-snippet you can adjust the output:
CSS = """.output {flex-direction: row;}"""HTML('<style>{}</style>'.format(CSS))
Now you can use display(df) with multiple dataframes in one single Jupyter cell:
Imagine you calculate two variables in one cell (even if it is not best practice 😉). Usually just the last result appears:
Just add this syntax and every result will be printed:
from IPython.core.interactiveshell import InteractiveShellInteractiveShell.ast_node_interactivity = "all"
With the standard configuration the Jupyter notebook covers 60% of your screen. With a short css command you can adjust the width (here 80%):
from IPython.core.display import display, HTMLdisplay(HTML("<style>.container { width:80% !important; }</style>"))
In the following screenshot you see the result:
Now we come to a very interesting topic: Jupyter extensions. As I already mentioned, a lot of people wrote extensions to optimize notebooks. I will show you some really useful extensions for data science. If you are using conda, just type:
conda install -c conda-forge jupyter_contrib_nbextensions
After you will have access to the Jupyter extensions. In the menu you usually start notebooks, you can also choose the extensions now (red-marked area). I will show you 7useful extensions (green-marked area). You can activate them by clicking the checkboxes:
If you write a lot of code, it can be confusing to read it after a while. With Codefolding you can hide indented code
If you activate the extensions Codefolding , a little triangle will appear in the upper left corner of each cell. You can click on it and indented code will be minimized.
Collapsible heading will make every project better readable. The extensions before helped you to structure the content of cells. With this extension you can give the whole notebook a better structure:
In the screenshot, you see the first part of the notebook I created for this article. The first headline is created with # and the second with ## . Similar to the Codefolding extension, you can use Collapsible heading with the little triangles in the upper left corner. You can click on it and all the content after a headline will be hidden:
It is a hierarchical system, h2 headlines will also be hidden if you click on the triangle of an h1 headline:
As soon as you are familiar with Python packages for data analysis, you want to improve the performance of your code. The Jupyter magic command %% timeit is great for getting the execution time of a cell because it will be executed 100 or 1000 times for example to get a valid mean of the execution time. For faster checks, the extension Execution time is great because it will always show the needed time in each output cell:
Sometimes I want to try something in a notebook. Usually, I create a cell and afterwards I am deleting it. But in larger projects, I sometimes forget to delete some junk and I got unnecessary commands in my notebook. A better practice is to use Scratchpad . This extension allows to create “throw-away” snippets:
As soon as you activate the extension you can open your Scratchpad with the little triangle in the right bottom corner.
Larger scripts take sometimes minutes or even hours to execute. With the extension Kernle idle you will receive a notification from your browser when the execution is finished:
You can disable the notification or change the timing in the top of your notebook. If you change the timing to 30, you will just receive notifications for scripts which need more than 30 seconds to execute.
In the screenshot below you can see a clean notebook without unnecessary menus. I like this look very much and you can get it by activating the zenmode mode:
I think it is important to learn the Python code and I think it ́s always better to document everything you do with the syntax. With the Table beautifier you can sort columns or the index by clicking on it. For fast checks this can be really useful:
Jupyter notebooks are great for analysis but there is still some potential to make them even better. As data scientist, there are some really useful css snippets and extensions to make your work more comfortable. I hope you enjoyed this article. If you know about any other useful extensions, feel free to post a comment.
If you enjoy Medium and Towards Data Science and didn’t sign up yet, feel free to use my referral link to join the community.