Skip to content Skip to sidebar Skip to footer

Temporarily Change Pandas’ Set_option For Displaying Dataframe

I'm using Jupyter notebook and imported many DataFrames and one of them contains very long strings. Thus, I'd like to temporarily change pandas' display option without affect the g

Solution 1:

pandas.option_context

with pd.option_context('display.max_colwidth', 220):
  print(df.head())

Consider d1

d1 = pd.DataFrame(dict(A=['brown', 'blue', 'blue'*20]))

print(d1)

                                                   A
0                                              brown
1                                               blue
2  bluebluebluebluebluebluebluebluebluebluebluebl...

You can see the column width is not long enough

with pd.option_context('display.max_colwidth', 220):
  print(d1.head())                                                           

    A
0                                                                             brown
1                                                                              blue
2  blueblueblueblueblueblueblueblueblueblueblueblueblueblueblueblueblueblueblueblue

To show the HTML

from IPython.displayimport display, HTMLwith pd.option_context('display.max_colwidth', 220):
  display(HTML(d1.to_html()))

enter image description here

Post a Comment for "Temporarily Change Pandas’ Set_option For Displaying Dataframe"