bytestorm bytestorm - 5 months ago 83
Python Question

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

Answer

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

from IPython.display import display
display(d)

display screenshot