LoginSignup
16
20

More than 5 years have passed since last update.

Jupyter Notebookでセルの途中でも値を出力するには

Last updated at Posted at 2017-02-11

まずは基本の動作から

言うまでもないことだが、セルの最後の値は自動的に出力される。
値によっては文字列よりも見やすい形で出力できる。
代表的なものがpandasのDataFrame。

Notebook
from bokeh.sampledata import iris   #Bokehに含まれるサンプルデータ
df = iris.flowers

df.head()

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

タプルにすることで2つ以上の値を出力できるが、出力は文字列になってしまう。

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関数

言わずと知れたprint関数。
これを使えばセルの途中でも値を出力できる。
ただし文字列しか出力できないのが残念なところ。

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

IPython.displayモジュールのdisplay関数

print関数の代わりにdisplay関数を使うと、セルの途中でもセルの最後の値と同じように見やすい形で出力できる。

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

すべての値を出力する設定

display関数を使うことも面倒なら、すべての値を出力するように設定することができる。
ただし、出力を行いたいセルとは別のセルで事前に設定しておく必要があることに注意。

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

元の状態に戻すにはast_node_interactivityにデフォルト値の'last_expr'を代入する。

Notebook
InteractiveShell.ast_node_interactivity = 'last_expr'
16
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
20