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

19

我想要打印一个 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行打印为类似以下内容:

你可以通过截断字符串来限制字符数,例如使用JavaScript的substr()或slice()函数。另外,CSS中的text-overflow属性可以用于在超出容器宽度时显示省略号。

```html
This is a long string that will be truncated.
```
3  Stop Pacifier Sucking without tears ...    This is a product well worth ...     5 

我希望这个限制能够适用于表格中的所有行。

3个回答

25

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

...

查看选项文档


谢谢Andy,我尝试使用这些选项与tabulate一起使用,但它们没有起作用。如果我想在一行中看到所有列,但是通过键入df(而不是使用表格)来切割行,则需要执行以下操作:pd.options.display.width = 200 pd.options.display.max_colwidth = 50 - TJ1
将宽度最大化,如果我理解你的意思是500。这将把所有内容放在同一行上。 - Andy Hayden

1

try this one:

pd.options.display.width = 1200
pd.options.display.max_colwidth = 100
pd.options.display.max_columns = 100

0
你可以像这样做:
df['column_name'] = df['column_name'].str[:width]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接