A real-world dataset might have a huge number of columns.
You can expand the output to see more columns of a pandas dataframe using the pd.set_option(‘display.max_columns’, 50) option.
Basic Example
pd.set_option('display.max_columns', n)
df
display.max_columns
– Property to setn
– denotes the number of columns you want to display. Use 50 if you want to display 50 columns
This tutorial teaches you how to expand the output to see more columns or see all columns of a pandas dataframe.
To select specific columns from the dataframe, read How To Select Columns From Pandas Dataframe
Table of Contents
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
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.417062 | 0.219623 | 0.921322 | 0.503798 | 0.982479 | 0.892796 | 0.248203 | 0.353815 | 0.726481 | 0.100264 | 0.510168 | 0.415028 | 0.947420 | 0.720434 | 0.924738 | 0.400641 | 0.304465 | 0.619013 | 0.310163 | 0.636735 | 0.151370 | 0.519218 | 0.287380 | 0.563320 | 0.214689 | 0.652611 | 0.046745 | 0.006324 | 0.068200 | 0.513099 | 0.020755 | 0.455717 | 0.697704 | 0.179328 | 0.032257 | 0.816988 | 0.196204 | 0.937047 | 0.505805 | 0.418193 | 0.80158 | 0.645451 | 0.827207 | 0.940999 | 0.715637 | 0.635426 | 0.160438 | 0.984641 | 0.950591 | 0.832376 |
1 | 0.437460 | 0.715467 | 0.365535 | 0.413917 | 0.390729 | 0.668190 | 0.933138 | 0.272375 | 0.669757 | 0.263731 | 0.554711 | 0.614270 | 0.620697 | 0.237893 | 0.510138 | 0.903928 | 0.807898 | 0.179799 | 0.374178 | 0.528347 | 0.302234 | 0.517696 | 0.211037 | 0.113548 | 0.968004 | 0.661091 | 0.723043 | 0.469664 | 0.516262 | 0.850608 | 0.069848 | 0.439313 | 0.090727 | 0.786028 | 0.422759 | 0.814950 | 0.254307 | 0.810177 | 0.256475 | 0.780853 | 0.52584 | 0.372847 | 0.531025 | 0.101307 | 0.324935 | 0.582926 | 0.781617 | 0.496048 | 0.162659 | 0.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 columns is
display.max_columns
.
The setting will remain the same for the complete session and reset when the kernel is restarted.
Code
The following code demonstrates how 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
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.417062 | 0.219623 | 0.921322 | 0.503798 | 0.982479 | 0.892796 | 0.248203 | 0.353815 | 0.726481 | 0.100264 | 0.510168 | 0.415028 | 0.947420 | 0.720434 | 0.924738 | 0.400641 | 0.304465 | 0.619013 | 0.310163 | 0.636735 | 0.151370 | 0.519218 | 0.287380 | 0.563320 | 0.214689 | 0.652611 | 0.046745 | 0.006324 | 0.068200 | 0.513099 | 0.020755 | 0.455717 | 0.697704 | 0.179328 | 0.032257 | 0.816988 | 0.196204 | 0.937047 | 0.505805 | 0.418193 | 0.80158 | 0.645451 | 0.827207 | 0.940999 | 0.715637 | 0.635426 | 0.160438 | 0.984641 | 0.950591 | 0.832376 |
1 | 0.437460 | 0.715467 | 0.365535 | 0.413917 | 0.390729 | 0.668190 | 0.933138 | 0.272375 | 0.669757 | 0.263731 | 0.554711 | 0.614270 | 0.620697 | 0.237893 | 0.510138 | 0.903928 | 0.807898 | 0.179799 | 0.374178 | 0.528347 | 0.302234 | 0.517696 | 0.211037 | 0.113548 | 0.968004 | 0.661091 | 0.723043 | 0.469664 | 0.516262 | 0.850608 | 0.069848 | 0.439313 | 0.090727 | 0.786028 | 0.422759 | 0.814950 | 0.254307 | 0.810177 | 0.256475 | 0.780853 | 0.52584 | 0.372847 | 0.531025 | 0.101307 | 0.324935 | 0.582926 | 0.781617 | 0.496048 | 0.162659 | 0.134785 |
Using pd.options.display.max_columns Attribute
Pandas allow you to directly set values for different options using the options
attribute.
- Use the
display.max_columns
option to display a specific number of columns. - It is similar to the
set_options()
method.
The setting will remain the same for the complete session and reset when the kernel is restarted.
Code
The following code demonstrates how to display 50 columns.
pd.options.display.max_columns = 35
df
All the 50 columns of the dataframe are printed.
Dataframe Will Look Like
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | … | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.417062 | 0.219623 | 0.921322 | 0.503798 | 0.982479 | 0.892796 | 0.248203 | 0.353815 | 0.726481 | 0.100264 | 0.510168 | 0.415028 | 0.947420 | 0.720434 | 0.924738 | 0.400641 | 0.304465 | … | 0.179328 | 0.032257 | 0.816988 | 0.196204 | 0.937047 | 0.505805 | 0.418193 | 0.80158 | 0.645451 | 0.827207 | 0.940999 | 0.715637 | 0.635426 | 0.160438 | 0.984641 | 0.950591 | 0.832376 |
1 | 0.437460 | 0.715467 | 0.365535 | 0.413917 | 0.390729 | 0.668190 | 0.933138 | 0.272375 | 0.669757 | 0.263731 | 0.554711 | 0.614270 | 0.620697 | 0.237893 | 0.510138 | 0.903928 | 0.807898 | … | 0.786028 | 0.422759 | 0.814950 | 0.254307 | 0.810177 | 0.256475 | 0.780853 | 0.52584 | 0.372847 | 0.531025 | 0.101307 | 0.324935 | 0.582926 | 0.781617 | 0.496048 | 0.162659 | 0.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
- The default setting will be used when the control is out of context.
You can use this method when changing the options temporarily.
Code
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
0 | 1 | 2 | … | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|
0 | 0.417062 | 0.219623 | 0.921322 | … | 0.984641 | 0.950591 | 0.832376 |
1 | 0.437460 | 0.715467 | 0.365535 | … | 0.496048 | 0.162659 | 0.134785 |
2 rows × 50 columns
Display All Columns in Pandas
To display all columns of the pandas dataframe,
- Get the number of columns in the dataframe using the
df.shape[1]
- 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
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.417062 | 0.219623 | 0.921322 | 0.503798 | 0.982479 | 0.892796 | 0.248203 | 0.353815 | 0.726481 | 0.100264 | 0.510168 | 0.415028 | 0.947420 | 0.720434 | 0.924738 | 0.400641 | 0.304465 | 0.619013 | 0.310163 | 0.636735 | 0.151370 | 0.519218 | 0.287380 | 0.563320 | 0.214689 | 0.652611 | 0.046745 | 0.006324 | 0.068200 | 0.513099 | 0.020755 | 0.455717 | 0.697704 | 0.179328 | 0.032257 | 0.816988 | 0.196204 | 0.937047 | 0.505805 | 0.418193 | 0.80158 | 0.645451 | 0.827207 | 0.940999 | 0.715637 | 0.635426 | 0.160438 | 0.984641 | 0.950591 | 0.832376 |
1 | 0.437460 | 0.715467 | 0.365535 | 0.413917 | 0.390729 | 0.668190 | 0.933138 | 0.272375 | 0.669757 | 0.263731 | 0.554711 | 0.614270 | 0.620697 | 0.237893 | 0.510138 | 0.903928 | 0.807898 | 0.179799 | 0.374178 | 0.528347 | 0.302234 | 0.517696 | 0.211037 | 0.113548 | 0.968004 | 0.661091 | 0.723043 | 0.469664 | 0.516262 | 0.850608 | 0.069848 | 0.439313 | 0.090727 | 0.786028 | 0.422759 | 0.814950 | 0.254307 | 0.810177 | 0.256475 | 0.780853 | 0.52584 | 0.372847 | 0.531025 | 0.101307 | 0.324935 | 0.582926 | 0.781617 | 0.496048 | 0.162659 | 0.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
pd.set_option('display.max_colwidth', 20)
df
Dataframe Will Look Like
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.363724 | 0.676707 | 0.721925 | 0.575342 | 0.566716 | 0.961884 | 0.073550 | 0.884787 | 0.317784 | 0.970323 | 0.280536 | 0.503097 | 0.007111 | 0.515447 | 0.105260 | 0.374613 | 0.600998 | 0.440877 | 0.886563 | 0.631789 | 0.428414 | 0.333583 | 0.010816 | 0.619702 | 0.250974 | 0.289465 | 0.246841 | 0.467938 | 0.892630 | 0.540848 | 0.826629 | 0.744478 | 0.854683 | 0.997073 | 0.950930 | 0.613686 | 0.934962 | 0.355072 | 0.050826 | 0.456950 | 0.838414 | 0.583197 | 0.671389 | 0.565268 | 0.263413 | 0.580325 | 0.914720 | 0.430985 | 0.614488 | 0.461278 |
1 | 0.191289 | 0.997060 | 0.481743 | 0.570579 | 0.439722 | 0.417660 | 0.874434 | 0.348448 | 0.804842 | 0.950400 | 0.973354 | 0.818711 | 0.198811 | 0.004225 | 0.256985 | 0.139528 | 0.142631 | 0.150044 | 0.316873 | 0.055435 | 0.288040 | 0.609995 | 0.405348 | 0.362378 | 0.340979 | 0.486746 | 0.614668 | 0.983170 | 0.109289 | 0.223767 | 0.785090 | 0.151063 | 0.791574 | 0.174479 | 0.765386 | 0.640319 | 0.648512 | 0.049203 | 0.847795 | 0.548358 | 0.763249 | 0.213180 | 0.457687 | 0.531022 | 0.474988 | 0.579902 | 0.845015 | 0.234135 | 0.491087 | 0.629529 |