Hello, I am Xiao Wu 🧐

Last time, I shared 5 tips for using Jupyter Notebook, and I got a good response in the comments section, so I summarized the rest.

This article continues with 5 interesting and useful tips for using Jupyter Notebook

1. Run the shell command

The shell command is used for text interaction with the computer.

For example, we were happily typing code in Jupyter Notebook when we discovered a library that needed to be installed. You need a command-line tool, but switching back and forth between shell and Jupyter is cumbersome.

In fact, in the notebook, you can directly run the shell command, the original command at the beginning of the English exclamation point! Can.

! pip install xlwingsCopy the code

This way we don’t have to leave the browser, just through an exclamation point! We have installed a third-party library (shell commands) in Jupyter Notebook, which is very convenient.

Note: Whale and other online Notebook sites, too! PIP is an additional installation of third-party libraries.

No, of course, shell commands are not the only way to install third-party libraries.

For example, when we are analyzing a bunch of data sets, we can use it to check the available data sets in the current working folder.

! dir *.csvCopy the code

2. Hide some output information

What does it mean to hide part of the output?

Let me give you an example.

When we use Matplotlib to draw the diagram, we will see the output in the blue circle below.

Similar output information, which is not needed in most cases, also seems “redundant.”

We add a semicolon at the end of the statement; , you can hide the output information.

The above sample code, change to this and then re-execute.

plt.scatter(x, y);
Copy the code

As you can see, the “redundant” output information is hidden.

3. Check the help information

A tool that can quickly view help information is a great help for beginners.

In Jupyter Notebook, in the Help menu, you’ll find direct links to online documentation for most of the commonly used libraries, including NumPy, Pandas, SciPy, and Matplotlib.

For more detailed libraries, methods, or variables, use? The Plus name makes it easy to view the help document.

Such as print? Check out the help documentation for Print.

4. LaTeX

In the previous article, I specifically recommended Markdown writing.

When you comment code, it’s inevitable that you’ll need to write formulas. Now that The Jupyter Notebook supports Markdown, we can write LaTeX in the Markdown cell, where it will be rendered as a formula using MathJax.

For example, execute the following text in a Cell in Markdown format:

$P(A \mid B) = \frac{P(B \mid A)P(A)}{P(B)}$
Copy the code

Press Enter to print the formula:

LaTeX format is used in Markdown. You can learn about it by yourself.

Markdown is a key part of the notebooks, so don’t forget to use it with your code!

5. Play WITH CSS in Jupyter

The table written by Pandas can be animated.

It says the output on the Jupyter Notebook environment is all HTML. So we just need to add CSS to make it interactive. Pandas provides ways to easily add styles so that we can do a lot of “tricks”

Here is another example to demonstrate (code from @Xiaoming)

The displacement

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(5.2), columns=['data1'.'data2'])
df['key1'] = ['a'.'a'.'b'.'b'.'e']
df['key2'] = ['one'.'two'.'one'.'two'.'one']
df.style.set_table_styles([
    {'selector': 'tr'.'props': [('transition-duration'.'1s')]},
    {'selector': 'tr:hover'.'props': [('transform'.'translate(90%)')]}])Copy the code

rotating

df.style.set_table_styles([
    {'selector': 'tr'.'props': [('transition-duration'.'1s')]},
    {'selector': 'tr:hover'.'props': [('transform'.' rotate(360deg)')]}])Copy the code

Specific principles:

First set the transition time for the target element, see www.runoob.com/css3/css3-t…

It then sets the mouse to move to a particular style of the target element using the pseudo-hover class.

The above two style changes use the two methods of 2D changes, see www.runoob.com/css3/css3-2…

Today we introduced 5 tips for using Jupyter Notebook. Stay tuned for the next two installments, which will cover magic commands and various mods.

Don’t forget to give this article a thumbs up at 👍 if you find some tips to increase your productivity/fun