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

≯℡__Kan透↙ 提交于 2020-04-10 09:40:32

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/56384952/printing-a-dataframe-from-a-function-nicely-as-in-jupyter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!