Pandas: Set number of max Rows and Cols shown in DataFrame

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
5 min

banner

# Table of Contents

  1. Pandas: Set the number of max Rows shown in a DataFrame
  2. Making sure all DataFrame rows are shown when printing
  3. Setting the maximum number of DataFrame columns to be shown
  4. Only setting the maximum number of rows and columns for a block of code
  5. Resetting the maximum number of rows or columns to the default values
  6. Set the number of max Rows and Cols shown using pd.options.display
  7. Pandas: Set the number of max Rows with DataFrame.head()

# Pandas: Set the number of max Rows shown in a DataFrame

You can use the display.max_rows option to set the number of max rows shown in a Pandas DataFrame or to show all rows.

The display.max_rows option defaults to 60 but can be increased to avoid switching to truncate view.

main.py
import pandas as pd # ๐Ÿ‘‡๏ธ Set max rows to 600 pd.set_option('display.max_rows', 600) # ๐Ÿ‘‡๏ธ or show all Rows (Unlimited) # pd.set_option('display.max_rows', None) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

set number of max rows in dataframe

The code for this article is available on GitHub

The pandas.set_option() method sets the value of the specified option.

The two arguments we passed to the method are the pattern and the value.

main.py
pd.set_option('display.max_rows', 600)
The display.max_rows option represents the maximum number of rows that are shown when you print a DataFrame.

If the max_rows value is exceeded, Pandas switches to truncate view.

Here is an example.

main.py
import pandas as pd # ๐Ÿ‘‡๏ธ set max rows to 4 pd.set_option('display.max_rows', 4) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

pandas switches to truncate view if max rows exceeded

The code for this article is available on GitHub

I set the max_rows option to 4, so only 4 rows are shown and the rest are truncated.

# Making sure all DataFrame rows are shown when printing

If you want to make sure all rows of the DataFrame are shown when printing, set the max_rows option to a high value, e.g. 1000 or simply use a value of 0 for an unlimited number of rows.

main.py
import pandas as pd # ๐Ÿ‘‡๏ธ set max rows to 1000 pd.set_option('display.max_rows', 1000) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

making sure all rows are shown when printing dataframe

The code for this article is available on GitHub

By default, the max_rows option is set to 60, which means that any DataFrame that has more than 60 rows gets truncated when printing.

You can also set the option to None, for an unlimited number of rows.

main.py
pd.set_option('display.max_rows', None)

# Setting the maximum number of DataFrame columns to be shown

There is also a display.max_columns option that you can use if you need to set the maximum number of columns to be shown.

main.py
import pandas as pd pd.set_option('display.max_rows', 1000) pd.set_option('display.max_columns', 15) df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

set maximum number of columns to be shown

The code for this article is available on GitHub

The display.max_columns option sets the maximum number of columns to be shown when printing a DataFrame.

If the value is exceeded, Pandas switches to a truncate view.

You can also set the option to None, for an unlimited number of columns.

main.py
pd.set_option('display.max_columns', None)

I've also written an article on how to set column widths in a Panda DataFrame.

# Only setting the maximum number of rows and columns for a block of code

If you only want to set the maximum number of DataFrame rows to be shown for a block of code, use the pandas.option_context() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) with pd.option_context('display.max_rows', 1000): print(df)

only setting the maximum number of rows for block of code

The code for this article is available on GitHub

You can also pass multiple options to the pandas.option_context() method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) with pd.option_context( 'display.max_rows', 1000, 'display.max_columns', 15 ): print(df)

passing multiple options to pandas option context

# Resetting the maximum number of rows or columns to the default values

If you want to reset the maximum number of rows or columns to the defaults, use the pandas.reset_option() method.

main.py
import pandas as pd # ๐Ÿ‘‡๏ธ set max rows to 600 pd.set_option('display.max_rows', 600) # ๐Ÿ‘‡๏ธ reset max_rows to default value pd.reset_option('display.max_rows') # ๐Ÿ‘‡๏ธ or reset all options pd.reset_option('all') df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

reset maximum number of rows or columns to default values

The code for this article is available on GitHub

# Set the number of max Rows and Cols shown using pd.options.display

You can also use the pandas.options.display attribute to set the number of max rows and columns shown when printing a DataFrame.

main.py
import pandas as pd pd.options.display.max_rows = 600 pd.options.display.max_columns = 15 df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) print(df)

set number of max rows and cols shown using pandas options display

The code for this article is available on GitHub

The pandas.options.display attribute returns a Display object on which we can directly set the max_rows and max_columns properties.

Once you start typing, your IDE should be able to help you with autocomplete.

ide should help you with auto complete

The benefit of using this approach is that you no longer have to hardcode string patterns like we did with pandas.set_option().

main.py
pd.set_option('display.max_rows', 600)

# Pandas: Set the number of max Rows with DataFrame.head()

If you only want to return the first N rows of a DataFrame, use the DataFrame.head method.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan', 'Ethan'], 'experience': [1, 1, 5, 7, 7], 'salary': [175.1, 180.2, 190.3, 205.4, 210.5], }) # name experience salary # 0 Alice 1 175.1 # 1 Bobby 1 180.2 # 2 Carl 5 190.3 print(df.head(3))

set max number of rows with dataframe head

The code for this article is available on GitHub

The DataFrame.head() method returns the first N rows of the DataFrame.

The only argument the method takes is the number of rows to be selected.

I've also written an article on how to set column widths in a Pandas DataFrame.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev