Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a dataframe from a function nicely as in Jupyter [duplicate]

I've seen a lot of very helpful posts on using prettyprint and such; they have been very helpful -- thanks.

What I'm wondering if there is anyway to print a dataframe from a function and have it output as "prettily" as Jupyter notebook does:

Jupyter notebook display

My main reason is I would like to use pandas's styling functions to highlight/shade. I also want to print from a function and not a Jupyter code box if possible as I have some packages I have created and I may want to spit out 2 or more dataframes in a call.

Using prettyprint or print() alone gives a purely text output:

>       Year      Month  Mean Maximum Temperature Albury  \ 
> 672   1955    January                             30.8    
> 673   1955   February                             27.9    
> 674   1955      March                             26.7
> 675   1955      April                             22.1  
> ....

I'd like the graphical output. Not using print(), e.g.

historic_dataframe

does nothing if within a function.

My thanks for your time.

like image 401
Climacus Avatar asked May 30 '19 20:05

Climacus


People also ask

How do I duplicate a DF?

The copy() method returns a copy of the DataFrame. By default, the copy is a "deep copy" meaning that any changes made in the original DataFrame will NOT be reflected in the copy.


1 Answers

Use display from IPython instead of print

import pandas as pd
from IPython.display import display

df = pd.DataFrame(pd.np.random.random((10, 10)))
display(df)

display docs

like image 105
dan_g Avatar answered Oct 02 '22 18:10

dan_g