How can I limit the column width in Pandas when displaying data frames, etc.? I know about display.max_colwidth , but this does not affect the column names. Also, I don't want to break names, but rather crop them.
If I set display.max_colwidth , it will probably limit the values โโand shorten them with ellipsis, but the column names remain long and unaffected. I do not see this anywhere in the documentation and have not seen it in large threads here or here . This is mistake? I know that I can truncate the names of the columns, but I want them to be long otherwise, just be truncated when displayed.
Just to understand what is going on:
Before
State area_harveste_2016_1000_acres area_harvested_2017_1000_acres yield_per_acr_2016_bushels yield_per_acre_2017_bushels 4 Alabama 315 235 120.0 165.0
Change width
pd.set_option("display.max_colwidth",5)
After
State area_harveste_2016_1000_acres area_harvested_2017_1000_acres yield_per_acr_2016_bushels yield_per_acre_2017_bushels 4 A... 315 235 1... 1...
Update
This is currently a confirmed issue with Pandas (as of 20.3 with problems # 7059 and # 16911 , until it is resolved, I wrote a work that sets unsets max_colwidth and also truncates with renaming. I would not do that although unanswered from this similar question
def pf(df, L=15): """Limit ENTIRE column width (including header)""" O = pd.get_option("display.max_colwidth") pd.set_option("display.max_colwidth", L) print(df.rename(columns=lambda x: x[:L - 3] + '...' if len(x) > L else x)) pd.set_option("display.max_colwidth", O)
ehiller
source share