baxx
baxx

Reputation: 4745

Set column width for pandas print out

given data as

In [403]: data=pd.DataFrame(
     ...: {
     ...: 'A':['hello there', 1],
     ...: 'B':['something', 44],
     ...: })

In [404]: data
Out[404]:
             A          B
0  hello there  something
1            1         44

I would like to be able to do something along the lines of

pd.set_output_width(4)

Which would result in the terminal output of the previous dataframe being alongt the lines of

In [404]: data
Out[404]:
    A      B
0  hell  some
1   1     44

Upvotes: 1

Views: 637

Answers (1)

jezrael
jezrael

Reputation: 863351

I test check display to 4, but it will be truncated with an ellipsis for length 4+:

with pd.option_context('display.max_colwidth', 3):
    print (data)
             A          B
0  hello there  something
1   1           44       

with pd.option_context('display.max_colwidth', 4):
    print (data)
     A    B
0  ...  ...
1    1   44

with pd.option_context('display.max_colwidth', 5):
    print (data)
      A     B
0  h...  s...
1     1    44

with pd.option_context('display.max_colwidth', 6):
    print (data)
       A      B
0  he...  so...
1      1     44

with pd.option_context('display.max_colwidth', 8):
    print (data)
         A        B
0  hell...  some...
1        1       44

I think close, what you need is slicing - with apply for all columns slicing:

print (data.astype(str).apply(lambda x: x.str[:4]))
      A     B
0  hell  some
1     1    44

Or with applymap for elementwise slicing:

print (data.astype(str).applymap(lambda x: x[:4]))
      A     B
0  hell  some
1     1    44

Upvotes: 2

Related Questions