Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print like jupyter notebook's default cell output

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.

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.

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 
like image 997
bytestorm Avatar asked Jun 21 '16 14:06

bytestorm


People also ask

How do you see the output of a cell in Jupyter Notebook?

Jupyter Notebook can print the output of each cell just below the cell. When you have a lot of output you can reduce the amount of space it takes up by clicking on the left side panel of the output. This will turn the output into a scrolling window.

What is %% capture in Jupyter?

Capturing Output With %%capture IPython has a cell magic, %%capture , which captures the stdout/stderr of a cell. With this magic you can discard these streams or store them in a variable.

How do I print multiple lines in a Jupyter Notebook?

Luckily, there's a Jupyter setting that you can change to print multiple outputs. The ast_node_interactivity setting allows you to choose which results are shown as outputs. By setting it to 'all', every assign and expression will be shown.


1 Answers

You can use IPython's display function to achieve that:

from IPython.display import display display(d) 

display screenshot

like image 50
k-nut Avatar answered Sep 17 '22 13:09

k-nut