像在 Jupyter 中一样从函数中很好地打印数据帧 [英] Printing a dataframe from a function nicely as in Jupyter

查看:33
本文介绍了像在 Jupyter 中一样从函数中很好地打印数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于使用prettyprint等非常有用的帖子;他们非常有帮助 - 谢谢.

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

我想知道是否有任何方法可以从函数中打印数据帧并使其像 Jupyter notebook 那样漂亮"地输出:

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:

我的主要原因是我想使用 pandas 的样式功能来突出显示/阴影.如果可能的话,我还想从函数而不是 Jupyter 代码框打印,因为我已经创建了一些包,我可能想在调用中吐出 2 个或更多数据帧.

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.

单独使用prettyprint或print()给出纯文本输出:

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  
> ....

我想要图形输出.不使用print(),例如

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

historic_dataframe

如果在函数内则什么也不做.

does nothing if within a function.

感谢您抽出宝贵时间.

推荐答案

使用 IPython 中的 display 而不是打印

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)

显示文档

这篇关于像在 Jupyter 中一样从函数中很好地打印数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆