How to widen output display to see more columns in Pandas dataframe?


When we work with large datasets in Pandas we often view and analyze data in a tabular format. When dealing with wide data frames 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. In this article, we will discuss various methods and techniques to widen the output display to see more columns.

The default Display settings

By default, Pandas restricts the number of columns displayed in order 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.

Method 1: Using the 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)

Here, pd.set_option('display.max_columns', None) is used to modify the display option in Pandas for the maximum number of columns to show. By setting it to None, it allows all columns in a DataFrame to be displayed without truncation.

Example

In the below example, we create a DataFrame df with 10 columns using the Pandas library. The columns are labeled as 'A' to 'J', and each column contains a list of three integer values. The code then sets the Pandas option display.max_columns to None, which ensures that all columns in the DataFrame will be displayed without truncation. Finally, the DataFrame df is printed, showing all the columns and their respective values.

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)

Output

    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

Method 2: Modifying the display.max_columns Option

We can directly modify the display.max_columns option using the pd.options.display attribute. By setting it to None, we can achieve the same result as in the previous method.

Syntax

pd.options.display.max_columns = None

Here, pd.options.display.max_columns = None is used to modify the display.max_columns option directly in Pandas. By setting it to None, all columns in a DataFrame will be displayed without truncation.

Example

In the below example, we create a DataFrame df with 10 columns using the Pandas library. The columns are labeled as 'A' to 'J', and each column contains a list of three integer values. Instead of using the pd.set_option method, this code modifies the display.max_columns option directly by assigning None to pd.options.display.max_columns. This ensures that all columns in the DataFrame will be displayed without truncation. Finally, the DataFrame df is printed, showing all the columns and their respective values.

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
pd.options.display.max_columns = None

print(df)

Output

     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

Method 3: Adjusting the Terminal/Console Width

Sometimes, the terminal or console window width may restrict the number of columns displayed. Adjusting the terminal width can be a simple solution to widen the output display. By increasing the width, more columns can be accommodated without truncation.

Example

In the below example,we import the Pandas library and the os module. We then set the terminal width to 150 columns by assigning the value '150' to the 'COLUMNS' environment variable using os.environ['COLUMNS']. Next, a DataFrame df is created with 15 columns. The column names are represented as strings of numbers ranging from 0 to 14, and each column contains a list of three repeating values of the respective column name. Finally, the DataFrame df is printed, displaying all 15 columns and their values..

import pandas as pd
import os

# Set the terminal width to 150 columns
os.environ['COLUMNS'] = '150'

# Create a DataFrame with 15 columns
data = {str(i): [i] * 3 for i in range(15)}
df = pd.DataFrame(data)

print(df)

Output

  0  1  2  3  4  5  6  7  8  9  10  11  12  13  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

Conclusion

In this article, we discussed how we can widen the output display to see more columns in Pandas Dataframe using various methods. We discussed using the pd.set_option method and modifying the display.max_columns option to display all columns. Additionally, we highlighted the significance of adjusting the terminal/console width to accommodate wider displays.

Updated on: 16-Oct-2023

87 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements