How to Show All Columns of a Pandas DataFrame


By default, Jupyter notebooks only displays 20 columns of a pandas DataFrame.

You can easily force the notebook to show all columns by using the following syntax:

pd.set_option('max_columns', None)

You can also use the following syntax to display all of the column names in the DataFrame:

print(df.columns.tolist())

Lastly, you can reset the default settings in a Jupyter notebook to only show 20 columns by using the following syntax:

pd.reset_option('max_columns')

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

Example: Show All Columns in Pandas DataFrame

Suppose we create a pandas DataFrame with 5 rows and 30 columns.

If we attempt to display the DataFrame in a Jupyter notebook, only 20 total columns will be shown:

import pandas as pd
import numpy as np

#create dataFrame with 5 rows and 30 columns
df = pd.DataFrame(index=np.arange(5), columns=np.arange(30))

#view dataFrame
df

To display all of the columns, we can use the following syntax:

specify that all columns should be shown
pd.set_option('max_columns', None)

#view DataFrame
df

Notice that all 30 columns are now shown in the notebook.

We can also use the following syntax to simply display all column names in the DataFrame:

print(df.columns.tolist())

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29]

To reset the default settings and only display a max of 20 columns, we can use the following syntax:

pd.reset_option('max_columns')

How to Show All Rows  in Pandas DataFrame

If you’d like to show every row in a pandas DataFrame, you can use the following syntax:

pd.set_option('max_rows', None) 

You can also specify a max number of rows to display in a pandas DataFrame. For example, you could specify that only a max of 10 rows should be shown:

pd.set_option('max_rows', 10) 

Additional Resources

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

Pandas: How to Get Cell Value from DataFrame
Pandas: Get Index of Rows Whose Column Matches Value
Pandas: How to Set Column as Index

Leave a Reply

Your email address will not be published. Required fields are marked *