Last updated: Apr 12, 2024
Reading time·4 min
Use the pandas.set_option()
method to set the column widths in a Pandas
DataFrame.
When passed the string "display.max_colwidth"
as the first parameter, the
method can be used to increase or decrease the column widths.
import pandas as pd pd.set_option('display.max_colwidth', 500) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'description': [ 'Content creator at https://example.com ABC 123', 'Content creator at https://bobbyhadz.com ABC 123', 'Content creator at https://google.com ABC 123' ], }) print(df)
As shown in
this section
of the docs, the default value for the display.max_colwidth
option is 50
characters.
repr
of Pandas DataFrames.If the column overflows, an ellipsis "..."
is shown in the output.
As previously mentioned, by default, the width of columns is 50 characters.
If you need to reset the column width, call the reset_option()
method.
import pandas as pd # Column width set to 500 characters pd.set_option('display.max_colwidth', 500) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'description': [ 'Content creator at https://example.com ABC 123', 'Content creator at https://bobbyhadz.com ABC 123', 'Content creator at https://google.com ABC 123' ], }) print(df) pd.reset_option('display.max_colwidth') # Column width set back to 50 characters (default)
The pandas.reset_option() method resets one or more options to their default value.
You can pass "all"
as an argument to the method to reset all options.
If you want to set the column widths in a Pandas DataFrame to unlimited, pass a
value of None
when calling pd.set_option()
method.
import pandas as pd pd.set_option('display.max_colwidth', None) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'description': [ 'Content creator at https://example.com ABC 123', 'Content creator at https://bobbyhadz.com ABC 123', 'Content creator at https://google.com ABC 123' ], }) print(df)
When the val
argument is set to None
in the call to pandas.set_option()
,
the width of columns is unlimited.
You can read more about using the pandas.set_option
method in
this section
of the docs.
When using the pandas.set_option()
method, the width of the columns is set for
the entire session or until you call the method with a different value.
If you want to temporarily set the widths of the columns, use the
option_context
function as a context manager.
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'description': [ 'Content creator at https://example.com ABC 123', 'Content creator at https://bobbyhadz.com ABC 123', 'Content creator at https://google.com ABC 123' ], }) with pd.option_context('display.max_colwidth', 500): print(df)
Make sure to print the DataFrame
inside the context manager (in the indented
code).
Once you exit the with
statement, the specified column width is no longer set.
display.expand_frame_repr
to False
If you still don't get the desired output, try setting the
display.expand_frame_repr
option to False
.
import pandas as pd pd.set_option('display.max_colwidth', None) pd.set_option('display.expand_frame_repr', False) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl'], 'description': [ 'Content creator at https://example.com ABC 123', 'Content creator at https://bobbyhadz.com ABC 123', 'Content creator at https://google.com ABC 123' ], }) print(df)
The display.expand_frame_repr
option determines whether to print out the full
DataFrame
repr for wide DataFrames across multiple lines.
Other attributes you might often have to set are:
display.max_rows
- If max_rows
is exceeded, switch to truncate view. Set
to None
if you want to set max_rows
to unlimited.display.max_columns
- If max_columns
is exceeded, switch to truncate view.
Set to None
to remove the limit.display.width
- Width of the display in characters. Can be set to None
for
Pandas to auto-detect the width when Python runs in a terminal.import pandas as pd pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000)
You can learn more about the related topics by checking out the following tutorials: