How To Expand The Output To See More Columns Of Pandas Dataframe – Definitive Guide

A real-world dataset might have a huge number of columns. You may need to see all the columns while printing the dataset using the Pandas Dataframe.

You can expand the output to see more columns of a pandas dataframe using the pd.set_option(‘display.max_columns’, 50) option.

This tutorial teaches you how to expand the output to see more columns or see all columns of a pandas dataframe

If You’re in Hurry…

You can use the display.max_colums property to display more columns in a pandas dataframe.

where,

  • display.max_columns – Property to set
  • n – denotes the number of columns you want to display. Use 50 if you want to display 50 columns
pd.set_option('display.max_columns', n)

df

If You Want to Understand Details, Read on…

Pandas will not show all columns by default when the dataset has more columns while printing. You need to set options to display more number of columns.

Different methods are available to set the maximum number of columns to be displayed.

To select columns from the dataframe, read How To Select Columns From Pandas Dataframe

Sample Dataframe

First, create a dataframe with 2 rows and 50 columns values and fill it with random values using np.random.random().

import numpy as np

import pandas as pd

df = pd.DataFrame(np.random.random(100).reshape(2, 50))

df

When you print the dataframe, only 20 columns are printed by default.

Dataframe Will Look Like

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
00.4170620.2196230.9213220.5037980.9824790.8927960.2482030.3538150.7264810.1002640.5101680.4150280.9474200.7204340.9247380.4006410.3044650.6190130.3101630.6367350.1513700.5192180.2873800.5633200.2146890.6526110.0467450.0063240.0682000.5130990.0207550.4557170.6977040.1793280.0322570.8169880.1962040.9370470.5058050.4181930.801580.6454510.8272070.9409990.7156370.6354260.1604380.9846410.9505910.832376
10.4374600.7154670.3655350.4139170.3907290.6681900.9331380.2723750.6697570.2637310.5547110.6142700.6206970.2378930.5101380.9039280.8078980.1797990.3741780.5283470.3022340.5176960.2110370.1135480.9680040.6610910.7230430.4696640.5162620.8506080.0698480.4393130.0907270.7860280.4227590.8149500.2543070.8101770.2564750.7808530.525840.3728470.5310250.1013070.3249350.5829260.7816170.4960480.1626590.134785

Now let us see how to use the options to display more columns.

Using Pandas set_Option Method

Pandas allow you to set values for different options using the set_option() method.

The option used to display more number of columns is display.max_columns.

Using this option, you can define how many columns must be printed in maximum. The setting will remain the same for the complete session, and it’ll be reset when the kernel is restarted.

Code

The following code demonstrates how to use the set_option() and the display.max_columns option to display 50 columns.

pd.set_option('display.max_columns', 50)

df

All the 50 columns in the Pandas dataframe are printed.

Dataframe Will Look Like

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
00.4170620.2196230.9213220.5037980.9824790.8927960.2482030.3538150.7264810.1002640.5101680.4150280.9474200.7204340.9247380.4006410.3044650.6190130.3101630.6367350.1513700.5192180.2873800.5633200.2146890.6526110.0467450.0063240.0682000.5130990.0207550.4557170.6977040.1793280.0322570.8169880.1962040.9370470.5058050.4181930.801580.6454510.8272070.9409990.7156370.6354260.1604380.9846410.9505910.832376
10.4374600.7154670.3655350.4139170.3907290.6681900.9331380.2723750.6697570.2637310.5547110.6142700.6206970.2378930.5101380.9039280.8078980.1797990.3741780.5283470.3022340.5176960.2110370.1135480.9680040.6610910.7230430.4696640.5162620.8506080.0698480.4393130.0907270.7860280.4227590.8149500.2543070.8101770.2564750.7808530.525840.3728470.5310250.1013070.3249350.5829260.7816170.4960480.1626590.134785

Using pd.options.display.max_columns Attribute

Pandas allow you to directly set values for different options using the options attribute.

You can use the display.max_columns option to display a specific number of columns.

It is similar to the set_options() method.

Using this option, you can define how many columns must be printed in maximum. The setting will remain the same for the complete session, and it’ll be reset when the kernel is restarted.

Code

The following code demonstrates how to use the options attribute and the display.max_columns option to display 50 columns.

pd.options.display.max_columns = 35

df

All the 50 columns of the dataframe are printed.

Dataframe Will Look Like

0123456789101112131415163334353637383940414243444546474849
00.4170620.2196230.9213220.5037980.9824790.8927960.2482030.3538150.7264810.1002640.5101680.4150280.9474200.7204340.9247380.4006410.3044650.1793280.0322570.8169880.1962040.9370470.5058050.4181930.801580.6454510.8272070.9409990.7156370.6354260.1604380.9846410.9505910.832376
10.4374600.7154670.3655350.4139170.3907290.6681900.9331380.2723750.6697570.2637310.5547110.6142700.6206970.2378930.5101380.9039280.8078980.7860280.4227590.8149500.2543070.8101770.2564750.7808530.525840.3728470.5310250.1013070.3249350.5829260.7816170.4960480.1626590.134785

2 rows × 50 columns

Using pd.option_context To Set Option Temporarily

Unlike the other options, the option_context() method allows you to set the option to a specific with statement context.

The options are valid only to that statement context, and the default setting will be used when the control is out of context.

You can use this method when changing the options temporarily.

Code

The following code demonstrates how to use the option_context() method to set the options temporarily.

with pd.option_context('display.max_columns', 20):
    print(df)

The print() statement prints the values of the 20 columns.

Output

         0         1         2         3         4         5         6   \
0  0.061784  0.472239  0.459510  0.605066  0.614997  0.985658  0.282726   
1  0.251828  0.922949  0.184618  0.261911  0.612386  0.024675  0.655262   

         7         8         9   ...        40        41        42        43  \
0  0.783858  0.535543  0.776357  ...  0.375716  0.911047  0.363114  0.659071   
1  0.074593  0.917032  0.028496  ...  0.616139  0.332934  0.678500  0.979174   

         44        45        46        47        48        49  
0  0.042372  0.570251  0.211773  0.451576  0.149772  0.332129  
1  0.843564  0.192778  0.842525  0.298098  0.793355  0.618525  

[2 rows x 50 columns]

Display Specific Number Of Columns

To print a specific number of columns, assign the number using the display.max_columns property.

Code

pd.options.display.max_columns = 6

df

Only the first six columns of the dataframe are printed.

Dataframe Will Look Like

012474849
00.4170620.2196230.9213220.9846410.9505910.832376
10.4374600.7154670.3655350.4960480.1626590.134785

2 rows × 50 columns

Display All Columns in Pandas

To display all columns of the pandas dataframe, you can get the number of columns in the dataframe using the df.shape[1] and set it to the display.max_columns property.

df.shape[1] returns the number of columns, whereas the df.shape[0] returns the number of rows.

Code

pd.options.display.max_columns = df.shape[1]

df

All the columns of the dataframe are printed.

Dataframe Will Look Like

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
00.4170620.2196230.9213220.5037980.9824790.8927960.2482030.3538150.7264810.1002640.5101680.4150280.9474200.7204340.9247380.4006410.3044650.6190130.3101630.6367350.1513700.5192180.2873800.5633200.2146890.6526110.0467450.0063240.0682000.5130990.0207550.4557170.6977040.1793280.0322570.8169880.1962040.9370470.5058050.4181930.801580.6454510.8272070.9409990.7156370.6354260.1604380.9846410.9505910.832376
10.4374600.7154670.3655350.4139170.3907290.6681900.9331380.2723750.6697570.2637310.5547110.6142700.6206970.2378930.5101380.9039280.8078980.1797990.3741780.5283470.3022340.5176960.2110370.1135480.9680040.6610910.7230430.4696640.5162620.8506080.0698480.4393130.0907270.7860280.4227590.8149500.2543070.8101770.2564750.7808530.525840.3728470.5310250.1013070.3249350.5829260.7816170.4960480.1626590.134785

Setting Maximum Column Width

You can use the max_colwidth property to set the maximum column width.

The default column width is 50.

When the column width overflows this set value, it is embedded with a “…” placeholder in the output.

Code

The following code demonstrates how to set the maximum column width option for the pandas dataframe.

pd.set_option('display.max_colwidth', 20)

df

Dataframe Will Look Like

012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
00.3637240.6767070.7219250.5753420.5667160.9618840.0735500.8847870.3177840.9703230.2805360.5030970.0071110.5154470.1052600.3746130.6009980.4408770.8865630.6317890.4284140.3335830.0108160.6197020.2509740.2894650.2468410.4679380.8926300.5408480.8266290.7444780.8546830.9970730.9509300.6136860.9349620.3550720.0508260.4569500.8384140.5831970.6713890.5652680.2634130.5803250.9147200.4309850.6144880.461278
10.1912890.9970600.4817430.5705790.4397220.4176600.8744340.3484480.8048420.9504000.9733540.8187110.1988110.0042250.2569850.1395280.1426310.1500440.3168730.0554350.2880400.6099950.4053480.3623780.3409790.4867460.6146680.9831700.1092890.2237670.7850900.1510630.7915740.1744790.7653860.6403190.6485120.0492030.8477950.5483580.7632490.2131800.4576870.5310220.4749880.5799020.8450150.2341350.4910870.629529

Conclusion

You’ve learned how to expand the output to see more columns or see all columns of a pandas dataframe.

You’ve also learned which option is to be used to set the option for the complete kernel session or to set the option for a specific context.

If you have any questions, please feel free to comment below.

You May Also Like

Leave a Comment