Unlock the Magic: 9 Essential Commands for Jupyter Notebook
Written on
Chapter 1: Introduction to Jupyter Notebook Magic Commands
Have you ever wondered if magic exists? If you haven't, you might want to reconsider that when you delve into the world of Jupyter Notebook magic commands. Though it may sound whimsical, Jupyter Notebook indeed offers a unique set of commands known as Magic Commands.
These commands, aptly named, allow you to perform specific tasks within your Jupyter Notebook environment. They are denoted by the % symbol followed by the desired command. While numerous Magic Commands are available, this article will highlight my top nine favorites. Let’s dive in!
Section 1.1: %who - Discover Your Variables
What does the command %who do? This particular magic command lists all the variables currently defined in your Jupyter Notebook environment. For instance, consider the following code snippet:
import seaborn as sns
df = sns.load_dataset('mpg')
a = 'simple'
b = 2
When you input %who in your Jupyter Notebook cell, it will display all existing variables.
As illustrated in the image above, all the variables, including those pre-existing in the environment, are shown. If you're interested in viewing a specific variable type, simply append the object type to the command, such as %who str.
Section 1.2: %timeit - Measure Execution Speed
The %timeit command is quite intriguing. It helps you assess the execution speed of your code by running it multiple times and calculating the average along with the standard deviation of the execution time. For example, try this:
import numpy as np
%timeit np.random.normal(size=1000)
By using the %timeit command, you can observe that the execution time varies around 341 ns for each run. This command is particularly useful for evaluating the stability of your code and its looping processes.
Chapter 2: More Magic Commands
In the first video, "Make Jupyter/IPython Notebook even more magical with cell magic extensions!", you'll discover additional ways to enhance your Jupyter experience through various cell magic extensions.
Section 2.2: %prun - Analyze Function Performance
The %prun command is another time-related magic command that evaluates the execution time of each function in your program. It provides a detailed table showing how many times each internal function was called, along with the time taken for each call and the cumulative time.
For example, execute:
%prun sns.load_dataset('mpg')
Section 2.3: %history - Review Your Commands
Have you ever lost track of the commands you've executed during your analysis? The %history command provides a log of your activity, allowing you to trace back your previous actions.
Simply type %history in your Jupyter Notebook cell and check the output.
Section 2.4: %pinfo - Get Detailed Information
When working with new objects or packages, you might want more detailed information. The %pinfo command helps you retrieve comprehensive details about your objects.
For instance, running %pinfo df will display all relevant information concerning the DataFrame object.
Section 2.5: %%writefile - Save Code as a Python File
Even though Jupyter Notebook may not be the ideal environment for development, you can still save your code as a Python file using the %%writefile command. For example:
%%writefile test.py
def number_awesome(x):
return 9
After executing this command, check your directory for the newly created Python file.
Section 2.6: %pycat - Read Python Files into Jupyter
If you want to read a Python file back into your Jupyter Notebook, the %pycat command can help. For example, to read the previous Python file:
%pycat test.py
A popup will display all the code within the Python file directly in your Jupyter Notebook.
Section 2.7: %quickref - Quick Reference for Magic Commands
Lastly, the %quickref command is invaluable, as it provides a comprehensive overview of all magic commands available in Jupyter Notebook. Running %quickref will present you with detailed explanations of each command.
Conclusion
Magic commands in Jupyter Notebook are powerful tools that can significantly enhance your productivity as a data scientist. The nine commands discussed here are essential for anyone looking to optimize their workflow: %who, %timeit, %store, %prun, %history, %pinfo, %%writefile, %pycat, and %quickref.
I hope this guide proves helpful! If you enjoyed this content and seek further insights into data science or the daily life of a data scientist, consider subscribing to my newsletter.
If you haven’t yet subscribed as a Medium Member, I encourage you to do so through my referral link.