Reputation: 1269
I have a dataframe with some rather long column values and would like them truncated when printing, so that it only prints the first 10 or 20 characters. This is the dataframe:
As you can see the trip_id is very long and shifts the other columns into the next row. Does someone know how to print this DF so that the trip_id will get shortened? I only found questions asking how to print non-truncated dataframes. I guess there is some option in the pd.option_context but I coulnd't find it yet.
Here's my printing code:
with pd.option_context('expand_frame_repr', True):
print(f"Trips_with_service_days (mit Fahrplantagen)")
print(f"{trips_with_service_days.head()[['trip_id', 'service_id', 'start_date', 'end_date', 'fahrplantage_ohne_ausfaelle']]}")
Thanks in advance.
Upvotes: 2
Views: 1378
Reputation: 1269
Thank you @Simone in the comment for the answer. The display.max_colwidth option does what I wanted. New code:
pd.set_option("max_colwidth", 35)
with pd.option_context('expand_frame_repr', True):
print(f"Trips_with_service_days (mit Fahrplantagen)")
print(f"{trips_with_service_days.head()[['trip_id', 'service_id', 'start_date', 'end_date', 'fahrplantage_ohne_ausfaelle']]}")
Upvotes: 1