A little-known Jupyter notebook using skills, today to share.

Hello, I'm Xiao Wu 🧐

Jupiter notebook is not only a friendly Python editor, but also an efficient tool integrating programming and writing! Recently, a little friend saw a screenshot of my Jupiter notebook and asked why you output the results without writing the variable name?

Hey, hey, at this time, I found that many people don't know this skill.

Text start~

Today, I recommend an unknown Jupyter Notebook skill.

Default output

Although I may edit Python files directly with Sublime Text at ordinary times, if it involves processing data, I'm still used to opening Jupyter Notebook.

Let's take the Pandas statement most commonly used in data processing as an example, as shown below.

import pandas as pd

data = {"Car": ["A", "B", "C", "D", "E", "F"],
        "Color": ["Red", "Yellow", "Black", "Green", "Black", "Red"],
        "Year": ["1990", "1980", "2003", "2000", "2001", "2004"],
        "Rating": ["2.5", "1.5", "3.8", "9.7", "8.9", "3.2"]}
df = pd.DataFrame(data)

After reading the dataset data, the DataFrame does not output data by default. We usually write the variable name df again to preview the data, or use the head() method to check the first five lines.

However, when we process a large amount of data, we need to write a df.head or DF to check the new DataFrame object for each operation (replacement, operation, conversion, splicing, etc.).

This is really troublesome. How to improve efficiency?

Change default output

Since Jupiter notebook is built on the IPython library, we can change the default output behavior by running the following code. It changes the way interactive shell s in IPython that Jupiter notebook relies on work.

from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = "last_expr_or_assign"

Its function is to display the calculation result of the last expression or the value used in the assignment statement.

After running these two lines of code, we will run the previous Pandas code and find:

As shown in the figure above, we no longer need to run df.head, and the created DataFrame object will be automatically displayed for us to check the data.

For other operations, the same output results are obtained.

In this way, we can directly display the DataFrame object after each operation.

Is it convenient~

In fact, in addition to the "last_expr_or_assign" option, we can also try other options.

ast_node_interactivity = Enum(['all', 'last', 'last_expr', 'none', 'last_expr_or_assign'],
        'all', 'last', 'last_expr' or 'none', 'last_expr_or_assign' specifying
        which nodes should be run interactively (displaying output from expressions).

For example, we will ast_ node_ After the interaction setting is changed to all,

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

You can output multiple results at one time.

put things right once and for all

However, I like laziness most. I'm certainly not happy to enter these two lines of code every time I create a new Notebook.

from IPython.core.interactiveshell import InteractiveShell 
InteractiveShell.ast_node_interactivity = "last_expr_or_assign"

So we can use this scheme once and for all:

On our computer /. IPython / profile_ In the default directory, create a directory named ipython_config.py file.

Then write the following code to ipython_config.py file.

c = get_config()
c.InteractiveShell.ast_node_interactivity = "last_expr_or_assign"

After creating this file, we will create any Notebook file, and the output behavior after opening will be fixed.

Suppress output

At this time, a small partner will ask, what if I don't want to output some code cells.

Ah, this...

Why don't you change it back?

Forget it, there's another way,

Just add a semicolon after your code to suppress the output.

As shown in the figure above, when we end with a semicolon, no more output is displayed.

Posted on Wed, 01 Dec 2021 12:06:49 -0500 by midi_mick