Python 如何像 jupyter notebook 的默认单元格输出一样打印

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37947496/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:07:38  来源:igfitidea点击:

How to print like jupyter notebook's default cell output

pythonpandasdataframeipythonjupyter-notebook

提问by bytestorm

I faced a problem while printing pandas dataframes in jupyter notebook. If the column names are really long it breaks the dataframe structure in different lines.

我在 jupyter notebook 中打印 pandas 数据帧时遇到了问题。如果列名真的很长,它会破坏不同行中的数据帧结构。

How can I print it like the way jupyter notebook does it by default(Shown in image - third cell)? As far as I know, only way to print the dataframe in bordered table style, you have to leave the variable name as the last command of the notebooks cell.

我怎样才能像 jupyter notebook 默认那样打印它(显示在图像中 - 第三个单元格)?据我所知,只有以带边框的表格样式打印数据框的方法,您必须将变量名称保留为笔记本单元格的最后一个命令。

enter image description here

在此处输入图片说明

Here's the code if you want to check it,

如果你想检查它,这是代码,

d = pd.DataFrame({'A1_column':[1, 2, 4], 'B1_column':['a', 'b', 'd'],
                 'A2_column':[1, 2, 4], 'B2_column':['a', 'b', 'd'],
                 'A3_column':[1, 2, 4], 'B3_column':['a', 'b', 'd'],
                 'A4_column':[1, 2, 4], 'B4_column':['a', 'b', 'd'],
                 'A5_column':[1, 2, 4], 'B5_column':['a', 'b', 'd'],
                 'A6_column':[1, 2, 4], 'B6_column':['a', 'b', 'd'],
                 'A7_column':[1, 2, 4], 'B7_column':['a', 'b', 'd']})
print(d)
d

回答by k-nut

You can use IPython's displayfunction to achieve that:

您可以使用 IPython 的display函数来实现:

from IPython.display import display
display(d)

display screenshot

显示截图