Home » Pandas: How to Set Column Widths

By default, Jupyter notebooks only display a maximum width of 50 for columns in a pandas DataFrame.

However, you can force the notebook to show the entire width of each column in the DataFrame by using the following syntax:

pd.set_option('display.max_colwidth', None)

This will set the max column width value for the entire Jupyter notebook session.

If you only want to temporarily display an entire column width, you can use the following syntax:

from pandas import option_context

with option_context('display.max_colwidth', None):
    print(df)

Lastly, you can reset the default column width settings in a Jupyter notebook by using the following syntax:

pd.reset_option('display.max_colwidth')

The following example shows how to use these functions in practice.

Example: Set Column Widths in Pandas

Suppose we create a pandas DataFrame with some extremely long strings in one column:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'string_column': ['A really really long string that contains lots of words', 'More words', 'Words', 'Cool words', 'Hey', 'Hi', 'Sup', 'Yo'],
                   'value_column': [12, 15, 24, 24, 14, 19, 12, 38]})


#view DataFrame
print(df)

                                       string_column  value_column
0  A really really long string that contains lots...            12
1                                         More words            15
2                                              Words            24
3                                         Cool words            24
4                                                Hey            14
5                                                 Hi            19
6                                                Sup            12
7                                                 Yo            38

By default, pandas cuts off the string_column to only have a width of 50.

To display the entire width of the column, we can use the following syntax:

#specify no max value for the column width
pd.set_option('display.max_colwidth', None)

#view DataFrame
print(df)

                                             string_column  value_column
0  A really really long string that contains lots of words            12
1                                               More words            15
2                                                    Words            24
3                                               Cool words            24
4                                                      Hey            14
5                                                       Hi            19
6                                                      Sup            12
7                                                       Yo            38

Notice that all of the text in the string_column is now shown.

Note that using this method will set the max column width for the entire Jupyter session.

To only temporarily display the max column width, we can use the following syntax:

from pandas import option_context

with option_context('display.max_colwidth', None):
    print(df)

                                             string_column  value_column
0  A really really long string that contains lots of words            12
1                                               More words            15
2                                                    Words            24
3                                               Cool words            24
4                                                      Hey            14
5                                                       Hi            19
6                                                      Sup            12
7                                                       Yo            38

To reset the default settings and only display a max width of 50 for each column, we can use the following syntax:

pd.reset_option('display.max_colwidth')

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Show All Columns of Pandas DataFrame
How to Show All Rows of a Pandas DataFrame
Pandas: How to Get Cell Value from DataFrame

You may also like