Contents

Set Max Rows for Display in pandas

By default, pandas display only 10 rows (first and last 5 rows and truncate middle section) for large DataFrame.

However, you can use the set_option function from pandas to set the maximum rows to display for a large DataFrame.

The basic syntax for the set_option function is:

Method 1: Display limited number of rows

# import package
import pandas as pd

pd.set_option('display.max_rows', n)

Where n is the number of rows that you want to display for pandas DataFrame.

For example, if you want to display the first 500 rows, you can replace n with 500.

Method 2: Display unlimited number of rows

# import package
import pandas as pd

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

If you use None, it will display all rows in the pandas DataFrame.

The following example explains how to use the pandas set_option function for setting the max number of rows for a DataFrame.

Example 1: Display limited number of rows

Create a random pandas DataFrame with 100 rows,

# load packages
import pandas as pd
import numpy as np

# crate random pandas dataframe
df = pd.DataFrame({'col1': np.random.rand(100), 
                   'col2': np.random.randint(1, 100, 100),
                   'col3': np.random.randn(100)})   

# view DataFrame
df

# output
       col1  col2      col3
0   0.821625    66 -0.601900
1   0.777521    84  0.961320
2   0.424575    53  1.806879
3   0.274048    73  0.997750
4   0.543021    67 -0.579300
..       ...   ...       ...
95  0.064159    96  0.755880
96  0.459158    34 -0.735654
97  0.959869    79  0.108148
98  0.153693    60 -0.821321
99  0.085131     4  1.517063

You can see that by default pandas DataFrame displays first and last 5 rows and truncates the middle section.

If you want to see all 100 rows, you can use the set_option function from pandas.

# display 100 rows
pd.set_option('display.max_rows', 100)

# check number of rows to display
print(pd.options.display.max_rows)

# output
100

# view DataFrame
df

# output 
# it will output 100 rows (not shown here)

You can also customize the display of a number of rows as per your requirement.

Tip
If you want to display less than 60 rows, you can use the head function. For example, if you want to display 50 rows, you can use df.head(50).

You can also reset the display of max rows to default using pd.reset_option('display.max_rows') code.

# reset to default
pd.reset_option('display.max_rows')

You can see that we have reset the max rows to display to 60 (default value) for pandas DataFrame.

Example 2: Display unlimited number of rows

You can also use the set_option function from pandas to display an unlimited number of rows.

This option is useful if you are working with different DataFrames with a variable number of rows.

Create a random pandas DataFrame with 500 rows,

# load packages
import pandas as pd
import numpy as np

# crate random pandas dataframe
df = pd.DataFrame({'col1': np.random.rand(500), 
                   'col2': np.random.randint(1, 100, 500),
                   'col3': np.random.randn(500)})   

# view DataFrame
df

# output
         col1  col2      col3
0    0.190732    82 -0.652320
1    0.806544    36 -0.379455
2    0.757255    45 -0.473587
3    0.988350    21  1.595246
4    0.919555    95 -2.948029
..        ...   ...       ...
495  0.230039    16  0.437081
496  0.383598    85 -0.642461
497  0.009036    14 -1.923618
498  0.549369     7  0.130875
499  0.047475    79 -1.236831

[500 rows x 3 columns]

If you want to see all 500 rows or do not want to put a limit, you can use the set_option function from pandas.

# display unlimited rows
pd.set_option('display.max_rows', None)

# check number of rows to display
print(pd.options.display.max_rows)

# output
None

# view DataFrame
df

# output 
# it will output 500 rows (not shown here)

This will display all 500 rows from the DataFrame.

You can also reset the display of max rows to default using pd.reset_option('display.max_rows') code.