15.1 C
London
Friday, July 5, 2024
HomePandas in PythonGeneral Functions in PythonHow to Show All Columns of a Pandas DataFrame

How to Show All Columns of a Pandas DataFrame

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories