How to set Column Widths in a Pandas DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
4 min

banner

# Table of Contents

  1. How to set Column Widths in a Pandas DataFrame
  2. Setting the column widths in a Pandas DataFrame to unlimited
  3. Temporarily setting the column widths in Pandas
  4. Setting display.expand_frame_repr to False

# How to set Column Widths in a Pandas DataFrame

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.

main.py
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)

set column widths in pandas dataframe

The code for this article is available on GitHub

As shown in this section of the docs, the default value for the display.max_colwidth option is 50 characters.

The option represents the maximum width in characters of a column in the 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.

main.py
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 code for this article is available on GitHub

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.

# Setting the column widths in a Pandas DataFrame to unlimited

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.

main.py
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)

setting column widths to unlimited

The code for this article is available on GitHub

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.

# Temporarily setting the column widths in Pandas

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.

main.py
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)

temporarily setting column widths in pandas

The code for this article is available on GitHub

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.

# Setting 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.

main.py
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)

set display expand frame repr option to false

The code for this article is available on GitHub

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.
main.py
import pandas as pd pd.set_option('display.max_rows', 500) pd.set_option('display.max_columns', 500) pd.set_option('display.width', 1000)

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.