[PYTHON] To output a value even in the middle of a cell with Jupyter Notebook

First from the basic operation

Needless to say, the last value of the cell is automatically output. Depending on the value, it can be output in a form that is easier to see than a character string. A typical one is pandas DataFrame.

Notebook


from bokeh.sampledata import iris   #Sample data included in Bokeh
df = iris.flowers

df.head()
スクリーンショット 2017-02-11 9.56.20.png

Two or more values can be output by making them tuples, but the output will be a character string.

Notebook


df.head(), df.tail()
(   sepal_length  sepal_width  petal_length  petal_width species
 0           5.1          3.5           1.4          0.2  setosa
 1           4.9          3.0           1.4          0.2  setosa
 2           4.7          3.2           1.3          0.2  setosa
 3           4.6          3.1           1.5          0.2  setosa
 4           5.0          3.6           1.4          0.2  setosa,
      sepal_length  sepal_width  petal_length  petal_width    species
 145           6.7          3.0           5.2          2.3  virginica
 146           6.3          2.5           5.0          1.9  virginica
 147           6.5          3.0           5.2          2.0  virginica
 148           6.2          3.4           5.4          2.3  virginica
 149           5.9          3.0           5.1          1.8  virginica)

print function

Needless to say, the print function. If you use this, you can output the value even in the middle of the cell. However, it is a pity that only character strings can be output.

Notebook


from bokeh.sampledata import iris
df = iris.flowers

print(df.head())
print(df.tail())
   sepal_length  sepal_width  petal_length  petal_width species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa
3           4.6          3.1           1.5          0.2  setosa
4           5.0          3.6           1.4          0.2  setosa
     sepal_length  sepal_width  petal_length  petal_width    species
145           6.7          3.0           5.2          2.3  virginica
146           6.3          2.5           5.0          1.9  virginica
147           6.5          3.0           5.2          2.0  virginica
148           6.2          3.4           5.4          2.3  virginica
149           5.9          3.0           5.1          1.8  virginica

Display function in the IPython.display module

If you use the display function instead of the print function, you can print in the middle of the cell in the same easy-to-read format as the last value in the cell.

Notebook


from IPython.display import display
from bokeh.sampledata import iris
df = iris.flowers

display(df.head(), df.tail())
スクリーンショット 2017-02-11 10.17.42.png

Setting to output all values

If you don't want to use the display function, you can set it to output all values. However, note that it is necessary to set in advance in a cell different from the cell where you want to output.

Notebook


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

Notebook


from bokeh.sampledata import iris
df = iris.flowers

df.head()
df.tail()
スクリーンショット 2017-02-11 10.17.42.png

To return to the original state, substitute the default value 'last_expr' for ʻast_node_interactivity`.

Notebook


InteractiveShell.ast_node_interactivity = 'last_expr'

Recommended Posts

To output a value even in the middle of a cell with Jupyter Notebook
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
The usual way to add a Kernel with Jupyter Notebook
Output the contents of ~ .xlsx in the folder to HTML with Python
I tried to create a Python script to get the value of a cell in Microsoft Excel
Rich cell output with Jupyter Notebook (IPython)
I tried to display the altitude value of DTM in a graph
Fill the browser with the width of Jupyter Notebook
Output in the form of a python array
[Python / Jupyter] Translate the comment of the program copied to the clipboard and insert it in a new cell
Feel free to write a test with nose (in the case of + gevent)
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
How to count the number of elements in Django and output to a template
How to make the font width of jupyter notebook put in pyenv equal width
How to get a list of files in the same directory with python
Drawing a tree structure with D3.js in Jupyter Notebook
[Python & SQLite] I tried to analyze the expected value of a race with horses in the 1x win range ①
Run the output code with tkinter, saying "A, pretending to be B" in python
Get the value of a specific key in a list from the dictionary type in the list with Python
How to identify the element with the smallest number of characters in a Python list?
A story about trying to introduce Linter in the middle of a Python (Flask) project
Memo to get the value on the html-javascript side with jupyter
How to import NoteBook as a module in Jupyter (IPython)
How to output a document in pdf format with Sphinx
About the garbled Japanese part of pandas-profiling in Jupyter notebook
Change the standard output destination to a file in Python
Reflect the virtual environment created with Miniconda in Jupyter notebook
How to get the last (last) value in a list in Python
I want to use a virtual environment with jupyter notebook!
How to see the contents of the Jupyter notebook ipynb file
Connect the Jupyter Notebook kernel to Spyder with Jupytext enabled
How to import NoteBook as a module in Jupyter (IPython)
The value of meta when specifying a function with no return value in Dask dataframe apply
Make a note of what you want to do in the future with Raspberry Pi
Get the value of a specific key up to the specified index in the dictionary list in Python
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
Output cell to file with Colaboratory
Output the key list included in S3 Bucket to a file
How to batch start a python program created with Jupyter notebook
Process the contents of the file in order with a shell script
How to determine the existence of a selenium element in Python
Find the optimal value of a function with a genetic algorithm (Part 2)
Git management of Jupyter notebook (ipynb) differences in easy-to-read with JupyterLab
How to check the memory size of a variable in Python
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I want to output the beginning of the next month with Python
Read the standard output of a subprocess line by line in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
[NNabla] How to remove the middle tier of a pre-built network
How to take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
I want to create a graph with wavy lines omitted in the middle with matplotlib (I want to manipulate the impression)
Output the result of morphological analysis with Mecab to a WEB browser compatible with Sakura server / UTF-8
[Python] The role of the asterisk in front of the variable. Divide the input value and assign it to a variable
I want to improve efficiency with Python even in the experimental system (5) I want to send a notification at the end of the experiment with the slack API
The story of a Parking Sensor in 10 minutes with GrovePi + Starter Kit
[Small story] How to save matplotlib graphs in a batch with Jupyter