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?
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
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:
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:
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:
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:
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 –
- Show all columns of Pandas DataFrame in Jupyter Notebook
- Pandas – Get dataframe summary with info()
- Insert Image in a Jupyter Notebook
- Undo cell deletion in Jupyter Notebook
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.