Expand Output Display to See More Columns in Pandas DataFrame

Display more (all) DataFrame columns: In this tutorial, we will learn how can we expand output display to see more (all) DataFrame columns with the help of Python program? By Pranit Sharma Last updated : April 18, 2023

In the real world, data is huge so is the dataset. While importing a dataset and converting it into DataFrame, if the number of columns is large, the default printing method does not print the entire columns. It compresses the rows and columns. In this article, we are going to learn how to expand the output display to see all the columns of DataFrame.

How to Expand Output Display to See More Columns in Pandas DataFrame?

To expand output display to see more DataFrame columns, you can use pandas.DataFrame.set_options() method, it allows to set the different properties such as setting maximum columns, maximum rows, etc. The display.max_columns() defines the total number of columns to be printed.

Let us understand with the help of an example.

Python Code to Expand Output Display to See More Columns in Pandas DataFrame

Example 1: Code with Default Settings

#  importing pandas package
import pandas as pd

# Creating an empty dictionary
d = {}

# Creating a large DataFrame of 20 columns
for i in range(1,21):
    d[i]=[i for i in range(0,11)]
df=pd.DataFrame(d)

# Printing default DataFrame
print(df)

Output

    1   2   3   4   5   6   7   8   9  ...  12  13  14  15  16  17  18  19  20
0    0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0
1    1   1   1   1   1   1   1   1   1 ...   1   1   1   1   1   1   1   1   1
2    2   2   2   2   2   2   2   2   2 ...   2   2   2   2   2   2   2   2   2
3    3   3   3   3   3   3   3   3   3 ...   3   3   3   3   3   3   3   3   3
4    4   4   4   4   4   4   4   4   4 ...   4   4   4   4   4   4   4   4   4
5    5   5   5   5   5   5   5   5   5 ...   5   5   5   5   5   5   5   5   5
6    6   6   6   6   6   6   6   6   6 ...   6   6   6   6   6   6   6   6   6
7    7   7   7   7   7   7   7   7   7 ...   7   7   7   7   7   7   7   7   7
8    8   8   8   8   8   8   8   8   8 ...   8   8   8   8   8   8   8   8   8
9    9   9   9   9   9   9   9   9   9 ...   9   9   9   9   9   9   9   9   9
10  10  10  10  10  10  10  10  10  10 ...  10  10  10  10  10  10  10  10  10

[11 rows x 20 columns]

Example 2: Code with display.max_columns Property

Here, we have defined 20 columns but not all the columns are printed, to overcome this problem, we will use the set_options('display.max_columns', cols_value) method.

#  importing pandas package
import pandas as pd

# Creating an empty dictionary
d = {}

# Creating a large DataFrame of 20 columns
for i in range(1,21):
    d[i]=[i for i in range(0,11)]
df=pd.DataFrame(d)

# Printing default DataFrame
print(df)

# Setting value of columns=20
pd.set_option('display.max_columns', 20)

# Printing modified DataFrame
print(df)

Output

    1   2   3   4   5   6   7   8   9  ...  12  13  14  15  16  17  18  19  20
0    0   0   0   0   0   0   0   0   0 ...   0   0   0   0   0   0   0   0   0
1    1   1   1   1   1   1   1   1   1 ...   1   1   1   1   1   1   1   1   1
2    2   2   2   2   2   2   2   2   2 ...   2   2   2   2   2   2   2   2   2
3    3   3   3   3   3   3   3   3   3 ...   3   3   3   3   3   3   3   3   3
4    4   4   4   4   4   4   4   4   4 ...   4   4   4   4   4   4   4   4   4
5    5   5   5   5   5   5   5   5   5 ...   5   5   5   5   5   5   5   5   5
6    6   6   6   6   6   6   6   6   6 ...   6   6   6   6   6   6   6   6   6
7    7   7   7   7   7   7   7   7   7 ...   7   7   7   7   7   7   7   7   7
8    8   8   8   8   8   8   8   8   8 ...   8   8   8   8   8   8   8   8   8
9    9   9   9   9   9   9   9   9   9 ...   9   9   9   9   9   9   9   9   9
10  10  10  10  10  10  10  10  10  10 ...  10  10  10  10  10  10  10  10  10

[11 rows x 20 columns]
    1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  \
0    0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   
1    1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   
2    2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   2   
3    3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   3   
4    4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   4   
5    5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   5   
6    6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   6   
7    7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   7   
8    8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   8   
9    9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   9   
10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10  10   

    19  20  
0    0   0  
1    1   1  
2    2   2  
3    3   3  
4    4   4  
5    5   5  
6    6   6  
7    7   7  
8    8   8  
9    9   9  
10  10  10  

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.