Python: limit the width of printed columns of pandas DataFrame
Asked Answered
B

3

19

I am trying to print a pandas DataFrame. One of the columns is too wide (it is a very long string). To print I am using tabulate library. But when it is printed it shows the whole content of all columns in one very long line. Here is what I see:

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

as you can see the line is too long. How can I limit the number of characters in the printed string? For example I would like that line 3 to be printed as something like this:

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

I want this limitation to be applied to all lines in the table.

Bartley answered 2/10, 2015 at 6:23 Comment(0)
S
25

There's max_colwidth and (terminal) 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 options docs.

Selfemployed answered 2/10, 2015 at 7:10 Comment(2)
Thanks Andy, I tried to use these options with tabulate, and they didn't work. If I want to see all columns in one line but lines are chopped by just typing df (not using tabular) then I need to do something like: pd.options.display.width = 200 pd.options.display.max_colwidth = 50Bartley
max the width very large, if I understand you, say 500. That'll put it all on the same line.Selfemployed
K
1

try this one:

pd.options.display.width = 1200
pd.options.display.max_colwidth = 100
pd.options.display.max_columns = 100
Kierakieran answered 8/12, 2018 at 16:38 Comment(0)
K
0

You can do something like this:

df['column_name'] = df['column_name'].str[:width]
Knurly answered 13/3, 2017 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.