Python:限制pandas DataFrame打印列的宽度

2023-12-20

我正在尝试打印 pandas DataFrame。 其中一列太宽(它是一根很长的字符串)。 要打印我正在使用tabulate图书馆。但是当它被打印时,它会在很长的一行中显示所有列的全部内容。这是我所看到的:

row  name                                                                                                review                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                rating

0  Planetwise Flannel Wipes                                                                            These flannel wipes are OK, but in my opinion not worth keeping.  I also ordered someImse Vimse Cloth Wipes-Ocean Blue-12 countwhich are larger, had a nicer, softer texture and just seemed higher quality.  I use cloth wipes for hands and faces and have been usingThirsties 6 Pack Fab Wipes, Boyfor about 8 months now and need to replace them because they are starting to get rough and have had stink issues for a while that stripping no longer handles.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  3
1  Planetwise Wipe Pouch                                                                               it came early and was not disappointed. i love planet wise bags and now my wipe holder. it keps my osocozy wipes moist and does not leak. highly recommend it.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        5
2  Annas Dream Full Quilt with 2 Shams                                                                 Very soft and comfortable and warmer than it looks...fit the full size bed perfectly...would recommend to anyone looking for this type of quilt                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       5
3  Stop Pacifier Sucking without tears with Thumbuddy To Love\'s Binky Fairy Puppet and Adorable Book  This is a product well worth the purchase.  I have not found anything else like this, and it is a positive, ingenious approach to losing the binky.  What I love most about this product is how much ownership my daughter has in getting rid of the binky.  She is so proud of herself, and loves her little fairy.  I love the artwork, the chart in the back, and the clever approach of this tool.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                5
4  Stop Pacifier Sucking without tears with Thumbuddy To Love\'s Binky Fairy Puppet and Adorable Book  All of my kids have cried non-stop when I tried to ween them off their paci

正如你所看到的,这条线太长了。 如何限制打印字符串中的字符数?例如,我希望将第 3 行打印为如下所示:

3  Stop Pacifier Sucking without tears ...    This is a product well worth ...     5 

我希望将此限制应用于表中的所有行。


There's max_colwidth和(终端)width:

In [11]: pd.options.display.width = 50

In [12]: pd.options.display.max_colwidth = 50

In [13]: df
Out[13]:
                                                   0  \
0                        0  Planetwise Flannel Wipes
1                           1  Planetwise Wipe Pouch
2             2  Annas Dream Full Quilt with 2 Shams
3  3  Stop Pacifier Sucking without tears with Th...
4  4  Stop Pacifier Sucking without tears with Th...

...

See the 选项文档 http://pandas.pydata.org/pandas-docs/stable/options.html.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python:限制pandas DataFrame打印列的宽度 的相关文章

随机推荐

  • 一旦使用pthread满足条件,for循环就不会终止

    当我发现 for 循环没有终止时 我正在编写一段多线程代码 起始代码大致是这样的 for int i V 1 tid i gt 0 i NTHREADS stuff V 和 NTHREADS 是常量 tid 是使用传递的线程 IDpthre
  • Jupyter:在不同的单元格中重新绘制

    我想做这样的事情 import matplotlib pyplot as plt matplotlib inline fig1 plt figure 1 plt plot 1 2 3 5 2 4 plt show 在一个单元格中 然后在另一
  • Swift 3 和 iOS 10 错误内存泄漏错误

    当与 iOS 10 和 Swift 3 一起使用时 Xcode 8 中似乎存在 错误的 内存泄漏错误 以下代码报告 Instruments 和 Xcode 8 内存调试器中的内存泄漏 class SomeClass NSObject var
  • Python将文本文件转换为字典

    我正在编写一个拼写检查函数 并且我有一个如下所示的文本文件 teh the cta cat dgo dog dya day frmo from memeber member 错误的拼写位于左侧 这将是我的密钥 正确的拼写位于右侧 我的值 d
  • 如何在 RxJava 中的动态列表上创建观察者?

    我需要在不断变化的数组上创建一个观察者 添加元素 我正在使用 Observable from Iterable 但它似乎在 ArrayList 上创建了 Observable 就像创建时一样 我需要通知观察者并在每次 ArrayList 添
  • 使用 ConcMarkSweepGC 的连续 CMS 收集

    在 Linux 上使用以下 JVM 选项启动 Tomcat 2cpu 4g RAM 800M ehcache 元素 Xmn512m Xmx2g Xms2g XX UseConcMarkSweepGC Xss128k XX PermSize
  • 我可以指定 MSBuild 标记的输出路径吗?

    是否可以为以下文件的输出指定不同的文件夹
  • 加入线程时处理异常的最佳方法

    出于某种原因 我对以下内容感到困惑 假设我有Thread A that 绝对地需要执行后Thread B已完成其处理 做到这一点的一种方法是Thread A加盟Thread B 简单的例子 public class MainThread p
  • android:使用默认视频播放器

    我有一个播放视频文件的应用程序 我一直在使用代码Videoview并开始Videoview手动播放视频文件 但是 我只是想知道我是否可以使用 android 的默认媒体播放器或视频播放器 而不是创建或使用VideoView播放文件 如果问题
  • 正确使用 IdUDPClient.ReceiveBuffer

    感谢您的帮助 我正在将旧版本的 Delphi 转换为 XE5 但我遇到了 Indy 组件的问题 需要使用IdUDPClient ReceiveBuffer 这是我的代码 while not Terminated do begin try l
  • 在整个站点使用 SSL [关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 我考虑的是为整个网站使用 SSL 而不是只选择几个页面进行 HTTPS 访问 这样做会有什么缺点 我希望允许社交服务登录 例如 Facebook
  • 从 Python 发送 HTTP POST 请求(尝试从 PHP 转换)

    我正在尝试将此代码片段从 PHP 转换为 Python 编程新手 但发现这样做很困难 我尝试转换的 PHP 如下 fp fsockopen whmcsurl 80 errno errstr 5 if fp querystring forea
  • 如何通过 __getattr__ 将参数传递给函数

    考虑以下代码示例 python 2 7 class Parent def init self child self child child def getattr self attr print Calling getattr attr i
  • ViewPager 上的左右箭头指示器

    我想在 ViewPager 上显示左右箭头 以指示滑动 我在 ViewPager 元素上添加了两个 ImageButton 但这些区域随后阻止 ViewPager 触发 滑动 我还希望按下这些箭头来触发片段相应地改变 简而言之 ImageB
  • 对具有 DI 依赖项的类进行玩笑模拟

    各种 Jest 文档显示了 自动 模拟 手动 模拟或ES6级 https jestjs io docs en es6 class mocks html模拟 在构造函数中实例化依赖项 但我想使用 DI IOC 并将依赖项注入到 ctor 中
  • 在资源中移动 .lproj 文件

    我正在尝试开始对一个项目进行本地化 并且我已经有了 Localized strings 的英文版本 我现在尝试将法语添加到该文件中 这将创建一个 fr lproj 文件夹 正如我所期望的那样 但是 它将它放在我的项目根目录中 我希望它位于我
  • 如何在 Firefox 中从剪贴板获取数据

    我想在元素上触发 onpaste 事件以检索剪贴板中的数据 我想检查剪贴板中是否存在图像并将其上传到服务器中 它在 Chrome 上完美运行 textarea 0 onpaste function event var items event
  • MVC .NET Core Web API XML 或 JSON

    我想创建一个以 XML 或 JSON 形式返回数据的 Web 应用程序 我该如何执行此操作 该模型 namespace ReturningJSONandXML Models public class SomeImportantInforma
  • VLCJ 创建多个视频面板

    我正在使用 vlcj 创建多个视频面板 对于每个视频窗口我应该添加代码 factory0 new MediaPlayerFactory mediaPlayer0 factory0 newEmbeddedMediaPlayer mediaPl
  • Python:限制pandas DataFrame打印列的宽度

    我正在尝试打印 pandas DataFrame 其中一列太宽 它是一根很长的字符串 要打印我正在使用tabulate图书馆 但是当它被打印时 它会在很长的一行中显示所有列的全部内容 这是我所看到的 row name review