Skip to main content

Python Pandas: How to Set and Control DataFrame Column Display Widths

When displaying Pandas DataFrames, especially those containing long strings or many columns, the default output formatting might truncate cell content or wrap the DataFrame in ways that make it hard to read. Pandas provides a flexible system of display options that allow you to control how DataFrames are rendered, including the maximum width of individual columns.

This guide explains how to use pandas.set_option() to adjust column display widths, set them to unlimited, and manage other related display settings.

Understanding Pandas Display Options

Pandas has a global options system that affects how various aspects of its objects (like DataFrames and Series) are displayed (e.g., in a terminal or Jupyter Notebook). These options can be configured using pandas.set_option(option_name, value).

For controlling column width, the key option is display.max_colwidth.

Setting Maximum Column Width (display.max_colwidth)

Default Behavior and Truncation

By default, display.max_colwidth is set to 50 characters. If the content of a cell in a column exceeds this width, Pandas will truncate the displayed string and show an ellipsis (...) to indicate that the full content is not visible.

import pandas as pd

data = {
'ID': [1, 2, 3],
'Short Text': ['Hello', 'World', 'Pandas'],
'Long Text': [
'This is a very long string that will likely exceed the default column width of 50 characters.',
'Another example of lengthy text that demonstrates truncation by default in Pandas DataFrames.',
'Short.'
]
}
df = pd.DataFrame(data)

print("--- DataFrame with Default Column Width (50 chars) ---")
print(df)

Output (Long Text column will be truncated):

--- DataFrame with Default Column Width (50 chars) ---
ID Short Text Long Text
0 1 Hello This is a very long string that will likely ex...
1 2 World Another example of lengthy text that demonstra...
2 3 Pandas Short.

Increasing or Decreasing Column Width

You can change this maximum width by setting the display.max_colwidth option.

import pandas as pd

data = {
'Long Text Column': [
'This string is moderately long and might be fully visible with a wider setting.',
'A much, much, much, much, much, much, much, much, much, much, much, much, much longer string that absolutely requires more width.'
]
}
df_long = pd.DataFrame(data)

# Default output (truncated)
print("--- Default (truncated) ---")
print(df_long)
print()

# ✅ Increase column width to 100 characters
pd.set_option('display.max_colwidth', 100)
print("--- display.max_colwidth set to 100 ---")
print(df_long)
print()

# Decrease column width (more aggressive truncation)
pd.set_option('display.max_colwidth', 20)
print("--- display.max_colwidth set to 20 ---")
print(df_long)

Output:

--- Default (truncated) ---
Long Text Column
0 This string is moderately long and might be fu...
1 A much, much, much, much, much, much, much, mu...

--- display.max_colwidth set to 100 ---
Long Text Column
0 This string is moderately long and might be fully visible with a wider setting.
1 A much, much, much, much, much, much, much, much, much, much, much, much, much longer string tha...

--- display.max_colwidth set to 20 ---
Long Text Column
0 This string is m...
1 A much, much, mu...

Setting Unlimited Column Width (None)

To display the full content of each column without any truncation, set display.max_colwidth to None.

import pandas as pd

data = {
'Very Long Text': [
'This is an extremely long piece of text that we want to see in its entirety, without any truncation whatsoever, regardless of how wide it makes the display.'
]
}
df_unlimited = pd.DataFrame(data)

# ✅ Set column width to unlimited
pd.set_option('display.max_colwidth', None) # Or -1 in some older versions

print("--- display.max_colwidth set to None (Unlimited) ---")
print(df_unlimited)

Output:

--- display.max_colwidth set to None (Unlimited) ---
Very Long Text
0 This is an extremely long piece of text that we want to see in its entirety, without any truncation whatsoever, regardless of how wide it makes the display.
note

Setting it to None tells Pandas to show the full width of the content for each cell.

Resetting to Default (reset_option)

To revert display.max_colwidth (or any other option) back to its default value, use pandas.reset_option().

import pandas as pd

# Set a custom width
pd.set_option('display.max_colwidth', 150)
print(f"Current max_colwidth: {pd.get_option('display.max_colwidth')}") # Output: 150

# ✅ Reset to default
pd.reset_option('display.max_colwidth')
print(f"max_colwidth after reset: {pd.get_option('display.max_colwidth')}") # Output: 50 (default)

Output:

Current max_colwidth: 150
max_colwidth after reset: 50
note

You can also reset all display options

pd.reset_option('all') # Use with caution

Temporarily Setting Column Widths (option_context)

pd.set_option() changes the setting globally for your current Python session (or until changed again). If you want to change an option only for a specific block of code, use pandas.option_context as a context manager.

import pandas as pd

data = {
'Description': ['A very long description that needs temporary full display for a specific print statement.']
}
df_temp = pd.DataFrame(data)

print(f"max_colwidth before context: {pd.get_option('display.max_colwidth')}") # Shows default or previous global setting

# ✅ Temporarily set max_colwidth to None within the 'with' block
with pd.option_context('display.max_colwidth', None, 'display.width', 1000):
print("\n--- Inside option_context (max_colwidth is None) ---")
print(df_temp)
print(f"max_colwidth inside context: {pd.get_option('display.max_colwidth')}") # Output: None

print(f"max_colwidth after context: {pd.get_option('display.max_colwidth')}") # Reverted to original setting

Output:

max_colwidth before context: 50

--- Inside option_context (max_colwidth is None) ---
Description
0 A very long description that needs temporary full display for a specific print statement.
max_colwidth inside context: None
max_colwidth after context: 50
note

The option is changed only within the with block and reverts to its previous value upon exiting the block. You can set multiple options in one option_context.

Controlling DataFrame Wrapping (display.expand_frame_repr)

Even with display.max_colwidth set to None, if your DataFrame is very wide (many columns or very wide individual columns), Pandas might still wrap it onto multiple lines to fit your terminal width. The display.expand_frame_repr option controls this.

  • True (default): Allows Pandas to display wide DataFrames across multiple "pages" or lines.
  • False: Attempts to print the DataFrame on a single line (or as few lines as possible horizontally), which can be useful if you want to see everything without horizontal breaks, even if it means scrolling horizontally in your terminal.
import pandas as pd

data = {
'Col A Long Name': ['Short value for A'],
'Col B Very Very Long Name': ['Short value for B'],
'Col C Extremely Unnecessarily Long Name For Demonstration': ['Short value for C']
}
df_wide = pd.DataFrame(data)

print("--- Default expand_frame_repr (True) with unlimited colwidth ---")
pd.set_option('display.max_colwidth', None)
pd.set_option('display.expand_frame_repr', True) # Explicitly setting default
print(df_wide)
# Output might wrap onto multiple lines depending on terminal width

# ✅ Set expand_frame_repr to False to prevent wrapping
print("\n--- expand_frame_repr set to False with unlimited colwidth ---")
pd.set_option('display.expand_frame_repr', False)
print(df_wide)

# Reset for subsequent examples
pd.reset_option('display.expand_frame_repr')
pd.reset_option('display.max_colwidth')

Output:

--- Default expand_frame_repr (True) with unlimited colwidth ---
Col A Long Name ... Col C Extremely Unnecessarily Long Name For Demonstration
0 Short value for A ... Short value for C

[1 rows x 3 columns]

--- expand_frame_repr set to False with unlimited colwidth ---
Col A Long Name Col B Very Very Long Name Col C Extremely Unnecessarily Long Name For Demonstration
0 Short value for A Short value for B Short value for C

Other Useful Display Options (max_rows, max_columns, width)

Along with display.max_colwidth, these options are frequently adjusted:

  • display.max_rows: Maximum number of rows to display. If exceeded, a truncated view (e.g., first and last few rows) is shown. Set to None for unlimited rows.
    pd.set_option('display.max_rows', 10)       # Show only 10 rows
    pd.set_option('display.max_rows', None) # Show all rows
  • display.max_columns: Maximum number of columns to display. If exceeded, a truncated view is shown. Set to None for unlimited columns.
    pd.set_option('display.max_columns', 5)     # Show only 5 columns
    pd.set_option('display.max_columns', None) # Show all columns
  • display.width: The width of the display in characters. Pandas uses this to decide how to format tables, wrap lines, etc. Set to None for Pandas to try and auto-detect the terminal width. For very wide DataFrames when expand_frame_repr is False, you might want to increase this.
    pd.set_option('display.width', 120)         # Set display width to 120 characters
    pd.set_option('display.width', None) # Auto-detect width

Example combining options:

import pandas as pd
import numpy as np

# Create a larger DataFrame
df_large = pd.DataFrame(np.random.rand(20, 10), columns=[f'Col_{i}' for i in range(10)])
df_large['Long_String_Col'] = ['A very long string example ' * 5] * 20

# Show all rows, all columns, full content of cells, and try to fit on one line if possible
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', 1000) # Give plenty of width
pd.set_option('display.expand_frame_repr', False) # Crucial for 'all on one line' effect

print("\n--- Large DataFrame with custom display settings ---")
print(df_large) # This would print the entire DataFrame

# Remember to reset if these global settings are not desired for subsequent code
pd.reset_option('all')

Conclusion

Pandas provides robust control over how DataFrames are displayed, which is essential for effective data exploration and presentation.

  • To control the truncation of cell content, use pd.set_option('display.max_colwidth', N) where N is the desired character width, or None for unlimited width.
  • Use pd.option_context(...) for temporary changes within a specific code block.
  • To manage how wide DataFrames are wrapped across lines, adjust pd.set_option('display.expand_frame_repr', False).
  • Complement these with display.max_rows, display.max_columns, and display.width for comprehensive control over DataFrame rendering.

By understanding and utilizing these display options, you can tailor Pandas output to best suit your analysis needs and terminal/notebook environment.