[转]设置Jupyter-Notebook表格打印多个变量的值

有一点已经众所周知。把变量名称或没有定义输出结果的语句放在cell的最后一行,无需print语句,Jupyter也会显示变量值。当使用Pandas DataFrames时这一点尤其有用,因为输出结果为整齐的表格。
鲜为人知的是,你可以通过修改内核选项ast_note_interactivity,使得Jupyter对独占一行的所有变量或者语句都自动显示,这样你就可以马上看到多个语句的运行结果了。

In [1]: from IPython.core.interactiveshell import InteractiveShell
        InteractiveShell.ast_node_interactivity = "all"
In [2]: from pydataset import data
        quakes = data('quakes')
        quakes.head()
        quakes.tail()
Out[2]:
        lat long    depth   mag stations
        1   -20.42  181.62  562 4.8 41
        2   -20.62  181.03  650 4.2 15
        3   -26.00  184.10  42  5.4 43
        4   -17.97  181.66  626 4.1 19
        5   -20.42  181.96  649 4.0 11
Out[2]:
        lat long    depth   mag stations
        996 -25.93  179.54  470 4.4 22
        997 -12.28  167.06  248 4.7 35
        998 -20.13  184.20  244 4.5 34
        999 -17.40  187.80  40  4.5 14
        1000    -21.59  170.56  165 6.0 119

如果你想在各种情形下(Notebook和Console)Jupyter都同样处理

创建文件~/.ipython/profile_default/ipython_config.py即可实现

c = get_config()
# Run all nodes interactively
c.InteractiveShell.ast_node_interactivity = "all"
原文地址:https://www.cnblogs.com/everfight/p/juypter-notebook.html