10 Jupyter’s Magic Commands Every Data Scientist Should Know

Turbocharge your workflow, debug faster, and run cleaner code with these underrated Jupyter tricks.

10 Jupyter’s Magic Commands Every Data Scientist Should Know

Unlock Hidden Powers in Your Notebooks

10 Jupyter’s Magic Commands Every Data Scientist Should Know

When you’re neck-deep in data exploration or model experimentation, even small productivity hacks can make a huge difference. That’s where Jupyter’s “magic commands” come in.

These are not your typical Python functions. Instead, they’re special commands that start with % or %%, designed to streamline your workflow inside Jupyter notebooks.

Yet despite being incredibly powerful, many data scientists either don’t know about them or barely scratch the surface.

In this article, we’ll explore 10 essential Jupyter magic commands that every data scientist should have in their toolbox. Whether you’re cleaning data, profiling performance, or managing external scripts, these tricks will save you time and elevate your notebook game.

Let’s get into it.

1. %time — Benchmark a Single Line of Code

Want to know how long a specific line of code takes to run? Wrap it with %time.

%time df = pd.read_csv("big_dataset.csv")

This gives you an immediate read on performance — no need to import time or write extra boilerplate.

Testing different loading or parsing strategies.
Comparing functions or algorithms for speed.

2. %%timeit — Get Reliable Execution Time Estimates

Unlike %time, %%timeit runs the code multiple times and gives you average execution time, which is much more reliable for benchmarking.

%%timeit 
df.apply(lambda row: row['a'] + row['b'], axis=1)

It’s especially useful for comparing multiple implementations without being misled by one-off CPU spikes.

Use %timeit -n 5 -r 3 to control number of loops (-n) and repeats (-r).

3. %who, %whos, and %who_ls — Inspect Current Variables

Debugging spaghetti notebooks? These commands let you peek into your namespace without printing everything manually.

%who        # Lists variable names 
%whos       # Gives details like type and size 
%who_ls     # Returns variable names as a list

No more scrolling endlessly to remember if you already loaded that DataFrame.

4. %run — Execute Python Scripts Inside Your Notebook

Sometimes, you want to keep reusable code in a .py file and call it as needed. %run lets you execute an external Python script in your current Jupyter context.

%run my_script.py

This is perfect for keeping your notebooks clean and modular.

You can pass arguments to the script:

%run my_script.py arg1 arg2

5. %load — Bring External Code Into Your Cell

If you want to load the contents of a Python file into a code cell (instead of executing it), %load is your friend.

%load my_script.py

It replaces the cell contents with the content of the file, ready for editing or running.

This is incredibly helpful for:

Reviewing functions before using them.
Annotating existing code.

6. %matplotlib inline — Visualize Plots Inside the Notebook

This command makes sure that matplotlib plots show up inline (i.e., within the notebook), not in separate pop-ups.

%matplotlib inline

It’s often included by default, but worth knowing why — it’s what makes visualizations like plt.plot() render neatly below the cell.

7. %store — Save Variables Between Sessions

Restarted your kernel and lost everything? %store helps you persist variables across notebook sessions.

foo = 42 
%store foo

Next time you open the notebook:

%store -r foo

This is a lifesaver for long-running experiments or large data objects you don’t want to reload every time.

8. %debug — Enter Interactive Debugger After Exception

When an exception occurs, just run %debug to enter an interactive debugging session.

%debug

You’ll be dropped into the Python debugger (pdb) and can inspect variables, step through the traceback, and identify the root cause faster than eyeballing the stack trace.

9. %%capture — Suppress Output (or Save It)

Got a noisy function that prints tons of logs you don’t care about? Use %%capture to suppress or capture its output.

%%capture output 
print("This won’t be displayed.")

You can later access it via:

print(output.stdout)

Use it to:

Clean up cluttered notebooks.
Store output for later analysis.

10. %alias — Create Shortcuts for Shell Commands

If you often type long terminal commands like !pip install, %alias can save you some keystrokes.

%alias pi pip install 
pi pandas numpy matplotlib

You can now use pi as a shortcut instead of typing pip install each time.

Great for:

Repetitive setup tasks.
Custom workflows with shell commands.

Bonus: %history — Retrieve Your Command History

Want to export or repeat a set of previous commands?

%history -n 1-10

This prints lines 1 through 10 with line numbers, which can be copied or reused easily. Perfect for documentation or audit trails.


Final Thoughts: Use Magic Commands Like a Pro

Jupyter magic commands are the kind of small wins that add up quickly. Once you get used to them, it’s hard to go back.

They help you:

  • Keep notebooks cleaner.
  • Run experiments faster.
  • Debug more effectively.
  • Organize workflows across scripts and sessions.

So next time you’re staring at a massive notebook, ask yourself: “Could a magic command make this better?”

Spoiler: The answer is probably yes.


What’s your favorite magic command?
Drop it in the comments or share your own productivity hacks — let’s learn from each other.

Photo by Julius Drost on Unsplash