Last updated: Apr 13, 2024
Reading timeยท5 min
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.
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)
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.
pd.set_option('display.max_rows', 600)
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.
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)
I set the max_rows
option to 4, so only 4 rows are shown and the rest are
truncated.
DataFrame
rows are shown when printingIf 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.
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)
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.
pd.set_option('display.max_rows', None)
DataFrame
columns to be shownThere is also a display.max_columns
option that you can use if you need to set
the maximum number of columns to be shown.
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)
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.
pd.set_option('display.max_columns', None)
I've also written an article on how to set column widths in a Panda DataFrame.
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.
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)
You can also pass multiple options to the pandas.option_context()
method.
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)
If you want to reset the maximum number of rows or columns to the defaults, use
the pandas.reset_option()
method.
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)
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
.
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)
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.
The benefit of using this approach is that you no longer have to hardcode string
patterns like we did with pandas.set_option()
.
pd.set_option('display.max_rows', 600)
DataFrame.head()
If you only want to return the first N rows of a DataFrame
, use the
DataFrame.head
method.
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))
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.
You can learn more about the related topics by checking out the following tutorials: