漂亮地打印整个Pandas系列/ DataFrame

本文讨论如何在终端或Jupyter Notebook中更美观地打印Pandas的Series和DataFrame,包括使用内置方法、自定义函数以及调整显示选项,以实现适当对齐、列边界和可能的颜色编码。帖子中提供了多种解决方案,包括使用`pd.options.display.max_rows`和`pd.set_option()`等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文翻译自:Pretty-print an entire Pandas Series / DataFrame

I work with Series and DataFrames on the terminal a lot. 我在终端上经常使用Series和DataFrames。 The default __repr__ for a Series returns a reduced sample, with some head and tail values, but the rest missing. 系列的默认__repr__返回减少的样本,具有一些头和尾值,但其余部分丢失。

Is there a builtin way to pretty-print the entire Series / DataFrame? 有没有一种内置方法可以漂亮地打印整个Series / DataFrame? Ideally, it would support proper alignment, perhaps borders between columns, and maybe even color-coding for the different columns. 理想情况下,它将支持适当的对齐方式,可能支持列之间的边界,甚至可能对不同列进行颜色编码。


#1楼

参考:https://stackoom.com/question/1IFBJ/漂亮地打印整个Pandas系列-DataFrame


#2楼

Sure, if this comes up a lot, make a function like this one. 当然,如果出现很多情况,请创建一个像这样的功能。 You can even configure it to load every time you start IPython: https://ipython.org/ipython-doc/1/config/overview.html 您甚至可以将其配置为在每次启动IPython时加载: https : //ipython.org/ipython-doc/1/config/overview.html

def print_full(x):
    pd.set_option('display.max_rows', len(x))
    print(x)
    pd.reset_option('display.max_rows')

As for coloring, getting too elaborate with colors sounds counterproductive to me, but I agree something like bootstrap's .table-striped would be nice. 至于颜色,过于精致的颜色听起来适得其反,但我同意类似bootstrap的.table-striped这样的东西会很好。 You could always create an issue to suggest this feature. 您总是可以创建一个问题来建议该功能。


#3楼

You can also use the option_context , with one or more options: 您还可以将option_context与一个或多个选项一起使用:

with pd.option_context('display.max_rows', None, 'display.max_columns', None):  # more options can be specified also
    print(df)

This will automatically return the options to their previous values. 这将使选项自动返回其先前的值。

If you are working on jupyter-notebook, using display(df) instead of print(df) will use jupyter rich display logic (like so) . 如果您正在使用jupyter-notebook,则使用display(df)而不是print(df)将使用jupyter-rich显示逻辑(像这样)


#4楼

After importing pandas, as an alternative to using the context manager, set such options for displaying entire dataframes: 导入熊猫后,作为使用上下文管理器的替代方法,请设置以下选项以显示整个数据框:

pd.set_option('display.max_columns', None)  # or 1000
pd.set_option('display.max_rows', None)  # or 1000
pd.set_option('display.max_colwidth', -1)  # or 199

For full list of useful options, see: 有关有用选项的完整列表,请参见:

pd.describe_option('display')

#5楼

No need to hack settings. 无需修改设置。 There is a simple way: 有一个简单的方法:

print(df.to_string())

#6楼

Try this 尝试这个

pd.set_option('display.height',1000)
pd.set_option('display.max_rows',500)
pd.set_option('display.max_columns',500)
pd.set_option('display.width',1000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值