Skip to Content

Pandas – Set (Increase or Decrease) Column Width

The Pandas library in Python comes with a number of useful methods and properties to manipulate tabular data via dataframes. The library also has options that you can modify to change the styling and display of the dataframes. In this tutorial, we will look at how to set the maximum column width of a pandas dataframe with the help of some examples.

How to increase the column width of a pandas dataframe?

increase max column width in pandas dataframe

You can use the pandas set_option() function to set (increase/decrease) the maximum column width, 'max_colwidth' display option in pandas dataframes.

The following is the syntax –

# change the maximum column width in pandas
pd.set_option('max_colwidth', NEW_COLUMN_WIDTH)

If you want the column to adjust to the width of the contents, set the max_colwidth to None.

Note that, the above syntax will alter the options for all the pandas dataframes in the current session (current active kernel session). If you wish to modify the column width only for a specific dataframe output use the pandas set_option() function in a confined context using pandas option_context().

Examples

Let’s now look at some examples of using the above syntax.

First, let’s create a dataframe with a column that contains data that is too wide to display with the default column width.

import pandas as pd

# taglines data
data = {
    "Company": ["Nike", "Redbull", "Disneyland", "BMW"],
    "Tagline": ["Just do it. "*3,
                "Redbull gives you wings. "*3,
                "The happiest place on Earth. "*3,
                "The ultimate driving machine. "*3]
}

# create dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

tagline dataframe with default max column width.

Here, we created a dataframe of some company names and their respective taglines.

Note that, the taglines are repeated so that they become long enough to not fit the default column width. You can see that on displaying the dataframe, some of the values in the “Tagline” column are truncated on the display since the column is not wide enough.

Let’s see the current column width settings.

# view the current settings for max_colwidth in pandas
pd.get_option('max_colwidth')

Output:

50

We get 50 as the current (default) column width.

Method 1 – Change the column width inside a specific context

If you do not want to globally change the column width, you can modify it inside a specific context using the pandas option_context() function.

For example, let’s now change the 'max_colwidth' to None so that column adjusts its width to the contents and then display the dataframe (all inside a specific context).

from pandas import option_context

# change column width inside a specific context
with option_context('display.max_colwidth', None):
    # display the dataframe
    display(df)

Output:

tagline dataframe with wide column width

Here, we create a specific context using the with keyword and changed the pandas display options specifically for that context using the option_context() function. We then used the IPython display() function to display the dataframe to the output.

Now, since we have not modified the global pandas display settings, if you try to display the dataframe outside the context, the resulting dataframe will be displayed with the default settings.

# display the dataframe
df

Output:

tagline dataframe with default max column width.

The “Tagline” column is not wide enough and some of the values are truncated in the output.

Method 2 – Change the global pandas max column width display settings

Alternatively, you can directly use the pandas set_option() function and change the 'max_colwidth'. This will apply the setting to all the pandas dataframes in the current kernel session.

# change pandas display options - set max_colwidth to None
pd.set_option('max_colwidth', None)
# display the dataframe
df

Output:

tagline dataframe with wide column width

You can see that the displayed column is wide enough to cover the contents of the “Tagline” column.

Since you’ve globally changed the pandas display settings. The options will persist for other dataframes as well.

# create a new dataframe
df2 = pd.DataFrame({
    "Company": ["Gillette", "Apple", "Visa"],
    "Tagline": [
        "The best a man can get. "*3,
        "Think different. "*3,
        "Everywhere you want to be. "*3
    ]
})

# display the dataframe
df2

Output:

new tagline dataframe with wide column width

Here, we created a new dataframe and you can see that it also displays wide columns without any truncation as the pandas max_colwidth display setting is set to None.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.