Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to widen output display to see more columns in Pandas dataframe?
When working with large datasets in Pandas, we often view and analyze data in a tabular format. When dealing with wide DataFrames containing numerous columns, the default display settings may truncate or hide some columns, making it difficult to fully explore and understand the data. To overcome this limitation, we can widen the output display in Pandas to ensure all columns are visible.
Default Display Settings
By default, Pandas restricts the number of columns displayed to fit the output within the available space. This behavior is controlled by the display.max_columns option, which determines the maximum number of columns to display.
import pandas as pd
# Create a DataFrame with many columns to see default behavior
data = {f'Col_{i}': [i*3, i*3+1, i*3+2] for i in range(1, 11)}
df = pd.DataFrame(data)
print("Default display:")
print(df)
Default display: Col_1 Col_2 Col_3 Col_4 ... Col_7 Col_8 Col_9 Col_10 0 3 6 9 12 ... 21 24 27 30 1 4 7 10 13 ... 22 25 28 31 2 5 8 11 14 ... 23 26 29 32 [3 rows x 10 columns]
Using pd.set_option() Method
The pd.set_option() method allows us to modify various display options in Pandas, including the number of columns to show. By setting the display.max_columns option to None, Pandas will display all columns in the DataFrame.
Syntax
pd.set_option('display.max_columns', None)
Example
import pandas as pd
# Create a DataFrame with 10 columns
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9],
'D': [10, 11, 12],
'E': [13, 14, 15],
'F': [16, 17, 18],
'G': [19, 20, 21],
'H': [22, 23, 24],
'I': [25, 26, 27],
'J': [28, 29, 30]}
df = pd.DataFrame(data)
# Set option to display all columns
pd.set_option('display.max_columns', None)
print(df)
A B C D E F G H I J 0 1 4 7 10 13 16 19 22 25 28 1 2 5 8 11 14 17 20 23 26 29 2 3 6 9 12 15 18 21 24 27 30
Using pd.options.display Method
We can directly modify the display.max_columns option using the pd.options.display attribute. This approach provides the same result as the previous method.
Syntax
pd.options.display.max_columns = None
Example
import pandas as pd
# Create a DataFrame with 10 columns
data = {'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9],
'D': [10, 11, 12],
'E': [13, 14, 15],
'F': [16, 17, 18],
'G': [19, 20, 21],
'H': [22, 23, 24],
'I': [25, 26, 27],
'J': [28, 29, 30]}
df = pd.DataFrame(data)
# Modify the display.max_columns option directly
pd.options.display.max_columns = None
print(df)
A B C D E F G H I J 0 1 4 7 10 13 16 19 22 25 28 1 2 5 8 11 14 17 20 23 26 29 2 3 6 9 12 15 18 21 24 27 30
Setting Display Width
Sometimes, the terminal or console window width may restrict the number of columns displayed. We can adjust the display width using the display.width option to accommodate more columns.
Example
import pandas as pd
# Create a DataFrame with 15 columns
data = {f'Col_{i}': [i] * 3 for i in range(15)}
df = pd.DataFrame(data)
# Set display options for wider output
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 200)
print(df)
Col_0 Col_1 Col_2 Col_3 Col_4 Col_5 Col_6 Col_7 Col_8 Col_9 Col_10 Col_11 Col_12 Col_13 Col_14 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Comparison of Methods
| Method | Syntax | Scope | Best For |
|---|---|---|---|
pd.set_option() |
pd.set_option('display.max_columns', None) |
Global | General use, clear syntax |
pd.options.display |
pd.options.display.max_columns = None |
Global | Direct attribute access |
| Display width | pd.set_option('display.width', 200) |
Global | When terminal width is limiting |
Conclusion
Use pd.set_option('display.max_columns', None) to display all columns in Pandas DataFrames. Combine with display.width settings when terminal width limits the output. These methods ensure complete data visibility for better analysis.
