Python Pandas: How to Control Max Rows and Columns Displayed (DataFrame Output)
When you print or display a Pandas DataFrame, especially a large one, Pandas by default truncates the output to show only a limited number of rows and columns (e.g., the first and last few). This is done to prevent overwhelming your console or display. However, you often need to see more (or all) of your data.
This guide explains how to use Pandas display options like display.max_rows
and display.max_columns
to control how many rows and columns are shown when a DataFrame is represented as a string (e.g., via print()
).
Understanding Pandas DataFrame Display Truncation
By default, Pandas has built-in limits to how much of a DataFrame it will display:
display.max_rows
(default: usually 60): If a DataFrame has more rows than this, Pandas shows a summarized view (e.g., first 5 and last 5 rows).display.max_columns
(default: usually 20): If a DataFrame has more columns than this, Pandas shows a summarized view (e.g., first and last few columns, with...
in between).display.max_colwidth
(default: 50): Max width of a single column's content.display.width
(default: 80): The width of the entire display in characters.
These options can be changed globally or temporarily.
Example DataFrame: We'll create a DataFrame that would normally be truncated.
import pandas as pd
import numpy as np
# Create a DataFrame that is likely to be truncated by default settings
rows = 70
cols = 25
data = np.random.randint(0, 100, size=(rows, cols))
df_large = pd.DataFrame(data, columns=[f'Col_{i}' for i in range(cols)])
print("--- Default Display (likely truncated) ---")
print(df_large)
Output:
--- Default Display (likely truncated) ---
Col_0 Col_1 Col_2 Col_3 Col_4 ... Col_20 Col_21 Col_22 Col_23 Col_24
0 89 86 36 94 85 ... 28 55 40 89 81
1 17 50 95 75 90 ... 24 34 60 40 63
2 79 91 88 92 89 ... 74 3 86 70 67
3 0 61 1 4 95 ... 44 36 53 53 45
4 18 89 99 61 77 ... 7 68 91 29 3
.. ... ... ... ... ... ... ... ... ... ... ...
65 43 46 11 45 25 ... 56 6 61 7 26
66 46 95 30 55 31 ... 20 61 40 24 45
67 91 65 51 21 4 ... 26 34 68 38 53
68 93 73 37 52 88 ... 99 20 20 60 36
69 37 8 88 86 61 ... 14 82 51 65 10
[70 rows x 25 columns]
Setting Maximum Number of Rows Displayed (display.max_rows
)
Use pd.set_option('display.max_rows', value)
to change this setting globally.
Increasing the Limit
Set it to a number higher than your DataFrame's row count if you want to see all rows up to that limit.
import pandas as pd
df_medium = pd.DataFrame({'A': range(15)})
print("--- Default display.max_rows (e.g., 10 by default in some environments) ---")
# To see truncation, temporarily set a low default for this block if needed
# with pd.option_context('display.max_rows', 10):
# print(df_medium)
# Output (if default is low, might show first 5 and last 5)
# ✅ Increase max_rows to show more, e.g., 100
pd.set_option('display.max_rows', 100)
print("\n--- display.max_rows set to 100 ---")
print(df_medium) # Now all 15 rows of df_medium should be visible
Output:
--- Default display.max_rows (e.g., 10 by default in some environments) ---
--- display.max_rows set to 100 ---
A
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
12 12
13 13
14 14
Showing ALL Rows (None
or a Large Number)
To ensure all rows are displayed regardless of the DataFrame's length, set display.max_rows
to None
. Alternatively, set it to a very large number.
import pandas as pd
df_example_rows = pd.DataFrame({'Data': range(100)})
# Show all rows by setting to None
pd.set_option('display.max_rows', None)
# Or set to a sufficiently large number
pd.set_option('display.max_rows', 5000)
Setting Maximum Number of Columns Displayed (display.max_columns
)
Similarly, use pd.set_option('display.max_columns', value)
to control column display.
import pandas as pd
import numpy as np
df_wide_example = pd.DataFrame(np.random.rand(5, 30), columns=[f'Col{i}' for i in range(30)])
# Default display.max_columns (e.g., 20) might truncate
print("--- Default display.max_columns (likely shows ... for columns) ---")
print(df_wide_example)
# ✅ Show all columns by setting to None
pd.set_option('display.max_columns', None)
print("--- display.max_columns set to None (all columns) ---")
print(df_wide_example) # This will print all 30 columns, likely wrapping lines
print()
# Or set to a specific number, e.g., 15
pd.set_option('display.max_columns', 15)
print("--- display.max_columns set to 15 ---")
print(df_wide_example) # Would show first ~7 and last ~7 columns with ...
Output:
--- Default display.max_columns (likely shows ... for columns) ---
Col0 Col1 Col2 ... Col27 Col28 Col29
0 0.760776 0.859549 0.049213 ... 0.183314 0.476875 0.596337
1 0.081624 0.741909 0.361607 ... 0.034948 0.036609 0.605831
2 0.466893 0.245632 0.127150 ... 0.644861 0.519082 0.296738
3 0.402415 0.551023 0.719620 ... 0.946012 0.338560 0.083753
4 0.226955 0.779744 0.818011 ... 0.992113 0.263270 0.453717
[5 rows x 30 columns]
--- display.max_columns set to None (all columns) ---
Col0 Col1 Col2 Col3 Col4 Col5 Col6 \
0 0.760776 0.859549 0.049213 0.233209 0.537421 0.046417 0.342944
1 0.081624 0.741909 0.361607 0.071956 0.520620 0.045402 0.759873
2 0.466893 0.245632 0.127150 0.481943 0.130257 0.837777 0.639024
3 0.402415 0.551023 0.719620 0.090962 0.697032 0.633765 0.369185
4 0.226955 0.779744 0.818011 0.617812 0.143987 0.223946 0.062204
Col7 Col8 Col9 Col10 Col11 Col12 Col13 \
0 0.820428 0.544824 0.813883 0.719931 0.386293 0.498839 0.009592
1 0.147981 0.471829 0.537544 0.066258 0.667628 0.572247 0.398745
2 0.679696 0.808240 0.214611 0.805182 0.854267 0.379686 0.990772
3 0.537248 0.900966 0.820438 0.521218 0.683831 0.922291 0.827511
4 0.101118 0.086092 0.471552 0.770238 0.723685 0.639691 0.685989
Col14 Col15 Col16 Col17 Col18 Col19 Col20 \
0 0.746218 0.577293 0.597841 0.439016 0.206982 0.872956 0.965181
1 0.748538 0.532820 0.962925 0.148121 0.821693 0.647482 0.651453
2 0.862877 0.545979 0.501727 0.832062 0.791085 0.374474 0.668263
3 0.053628 0.335312 0.171996 0.889048 0.634134 0.007504 0.061592
4 0.627974 0.739581 0.253321 0.878191 0.808546 0.905331 0.087337
Col21 Col22 Col23 Col24 Col25 Col26 Col27 \
0 0.562792 0.677700 0.170359 0.138747 0.286603 0.741607 0.183314
1 0.934695 0.045318 0.066513 0.240516 0.754705 0.908138 0.034948
2 0.245244 0.813164 0.074500 0.636282 0.285742 0.026493 0.644861
3 0.585408 0.587802 0.967406 0.420088 0.001149 0.612452 0.946012
4 0.624579 0.300330 0.196629 0.449119 0.856473 0.324761 0.992113
Col28 Col29
0 0.476875 0.596337
1 0.036609 0.605831
2 0.519082 0.296738
3 0.338560 0.083753
4 0.263270 0.453717
--- display.max_columns set to 15 ---
Col0 Col1 Col2 Col3 Col4 Col5 Col6 ... \
0 0.760776 0.859549 0.049213 0.233209 0.537421 0.046417 0.342944 ...
1 0.081624 0.741909 0.361607 0.071956 0.520620 0.045402 0.759873 ...
2 0.466893 0.245632 0.127150 0.481943 0.130257 0.837777 0.639024 ...
3 0.402415 0.551023 0.719620 0.090962 0.697032 0.633765 0.369185 ...
4 0.226955 0.779744 0.818011 0.617812 0.143987 0.223946 0.062204 ...
Col23 Col24 Col25 Col26 Col27 Col28 Col29
0 0.170359 0.138747 0.286603 0.741607 0.183314 0.476875 0.596337
1 0.066513 0.240516 0.754705 0.908138 0.034948 0.036609 0.605831
2 0.074500 0.636282 0.285742 0.026493 0.644861 0.519082 0.296738
3 0.967406 0.420088 0.001149 0.612452 0.946012 0.338560 0.083753
4 0.196629 0.449119 0.856473 0.324761 0.992113 0.263270 0.453717
[5 rows x 30 columns]
Temporary Display Settings with pd.option_context()
If you only want to change display settings for a specific block of code, use pd.option_context()
as a context manager. The settings revert to their previous values automatically upon exiting the with
block.
import pandas as pd
df_temp_context = pd.DataFrame({'Data': range(70), 'Info': list('abcdef'*10 + 'abcdefghij')})
print(f"Max rows before context: {pd.get_option('display.max_rows')}")
print(f"Max cols before context: {pd.get_option('display.max_columns')}")
with pd.option_context('display.max_rows', 5, 'display.max_columns', 1):
print("--- Inside option_context (max_rows=5, max_columns=1) ---")
print(df_temp_context)
print(f"Max rows inside context: {pd.get_option('display.max_rows')}")
print(f"Max cols inside context: {pd.get_option('display.max_columns')}")
print(f"Max rows after context: {pd.get_option('display.max_rows')}") # Back to original
print(f"Max cols after context: {pd.get_option('display.max_columns')}") # Back to original
Output:
Max rows before context: 60
Max cols before context: 0
--- Inside option_context (max_rows=5, max_columns=1) ---
Data ...
0 0 ...
1 1 ...
.. ... ...
68 68 ...
69 69 ...
[70 rows x 2 columns]
Max rows inside context: 5
Max cols inside context: 1
Max rows after context: 60
Max cols after context: 0
Resetting Options to Default Values (pd.reset_option()
)
To revert a specific option or all options back to their Pandas defaults:
import pandas as pd
# Set some custom options
pd.set_option('display.max_rows', 200)
pd.set_option('display.max_columns', 50)
print(f"Custom max_rows: {pd.get_option('display.max_rows')}")
print(f"Custom max_columns: {pd.get_option('display.max_columns')}")
# ✅ Reset a specific option to its default
pd.reset_option('display.max_rows')
print(f"max_rows after reset: {pd.get_option('display.max_rows')}")
# ✅ Reset all display options (or a group like 'display')
pd.reset_option('all') # Resets ALL pandas options
pd.reset_option('display') # Resets all options starting with 'display.'
print(f"max_columns after resetting 'display': {pd.get_option('display.max_columns')}")
Output:
Custom max_rows: 200
Custom max_columns: 50
max_rows after reset: 60
max_columns after resetting 'display': 0
Alternative Syntax: pd.options.display.attribute
You can also access and set display options as attributes of pd.options.display
. This can be more discoverable with IDE autocompletion.
import pandas as pd
import numpy as np
# ✅ Set max_rows using attribute access
pd.options.display.max_rows = 500
pd.options.display.max_columns = 30
df_options_attr = pd.DataFrame(np.random.rand(10, 5)) # Example DataFrame
print("--- Using pd.options.display attributes ---")
print(df_options_attr) # Will reflect the 500 rows, 30 columns settings
print(f"Current pd.options.display.max_rows: {pd.options.display.max_rows}") # 500
# Resetting still typically uses pd.reset_option()
pd.reset_option('display.max_rows')
pd.reset_option('display.max_columns')
Output:
--- Using pd.options.display attributes ---
0 1 2 3 4
0 0.283069 0.932245 0.330104 0.587492 0.371314
1 0.777149 0.460749 0.256316 0.430297 0.757702
2 0.315404 0.417929 0.559148 0.080082 0.870593
3 0.000260 0.584651 0.151056 0.624236 0.655744
4 0.889543 0.148487 0.784346 0.271904 0.212780
5 0.624935 0.365785 0.834500 0.645679 0.188810
6 0.372870 0.349309 0.642280 0.463366 0.418648
7 0.983634 0.713502 0.465752 0.092636 0.108164
8 0.260189 0.700915 0.201867 0.228290 0.659885
9 0.472476 0.176511 0.776265 0.007179 0.654260
Current pd.options.display.max_rows: 500
Viewing First N Rows with DataFrame.head()
(Different from Display Options)
It's important to distinguish display.max_rows
from df.head(N)
.
pd.set_option('display.max_rows', N)
affects howprint(df)
or simplydf
(in a Jupyter cell) displays any DataFrame.df.head(N)
returns a new DataFrame containing only the firstN
rows ofdf
. This is for subsetting data, not just controlling display.
import pandas as pd
df_for_head = pd.DataFrame({'Numbers': range(100)})
# Get a DataFrame with only the first 3 rows
first_3_rows = df_for_head.head(3)
print("--- Using df.head(3) ---")
print(first_3_rows)
# display.max_rows still applies when printing 'first_3_rows' if it were longer than the limit.
Output:
--- Using df.head(3) ---
Numbers
0 0
1 1
2 2
Similarly, df.tail(N)
gets the last N rows.
Conclusion
Pandas provides flexible control over how DataFrames are displayed, which is essential for working with datasets of various sizes.
- To control the maximum number of rows shown, use
pd.set_option('display.max_rows', N)
(orNone
for all). - To control the maximum number of columns shown, use
pd.set_option('display.max_columns', N)
(orNone
for all). - Use
pd.option_context(...)
for temporary, localized changes to display settings. - Revert to defaults using
pd.reset_option(...)
. - The
pd.options.display.attribute
syntax is an alternative way to set these options.
By adjusting these settings, you can ensure that DataFrame output is informative and manageable, whether you need a quick overview or a detailed view of all your data.