In situations where the pandas DataFrame contains too many columns, the default display settings don’t allow all the columns to be shown in the output. Just a few first and last columns would be displayed and the middle columns would be compressed as “…” sign. In this article, we will discuss multiple approaches on how to expand the output display to see more columns in such situations.
Table of Contents
Introduction
To quickly get started, let’s create a sample DataFrame to experiment. We’ll use the pandas and NumPy library with some random data.
# importing library import numpy as np import pandas as pd # random DataFrame df = pd.DataFrame(np.random.random(80).reshape(2, 40)) print (df)
Contents of the created dataframe are,
0 1 2 3 ... 36 37 38 39 0 0.622675 0.060146 0.604744 0.710405 ... 0.571265 0.567789 0.761263 0.985691 1 0.601135 0.687421 0.537218 0.715122 ... 0.865830 0.680659 0.340409 0.228477 [2 rows x 40 columns]
In the above output, all the columns between 4 to 35 are minimized, and shown as “…”. In order to expand the display, let’s look at different methods.
Expand display to show all columns using pandas.set_option function
The set_option() functionality in pandas offers all the flexibility to change the default display settings. To change the default number of columns displayed, use the “max_columns” argument from the display.
# change the max_columns value to 40 pd.set_option("display.max_columns", 40) print (df)
Now after calling the set_option() with 40 as max_columns, if we print any DataFrame then at max it will display 40 columns of the DataFrame. Let’s see a complete example,
Frequently Asked:
- Read a CSV file without a header in Pandas
- Pandas Tutorial #14 – Sorting DataFrame
- Select Rows by Timestamp in Pandas
- How to read a large CSV file with pandas?
# importing library import numpy as np import pandas as pd # random DataFrame df = pd.DataFrame(np.random.random(80).reshape(2, 40)) # change the max_columns value to 40 pd.set_option("display.max_columns", 40) print (df)
Output
0 1 2 3 4 5 6 \ 0 0.099547 0.317654 0.696267 0.500405 0.662395 0.347217 0.919962 1 0.183956 0.093377 0.914585 0.824104 0.178390 0.714917 0.740702 7 8 9 10 11 12 13 \ 0 0.792616 0.856906 0.470693 0.828003 0.167716 0.705077 0.900042 1 0.099741 0.748058 0.281502 0.632023 0.428459 0.123901 0.231481 14 15 16 17 18 19 20 \ 0 0.753212 0.838928 0.762214 0.256567 0.874984 0.396890 0.600915 1 0.649959 0.205741 0.174359 0.349932 0.758043 0.092794 0.180718 21 22 23 24 25 26 27 \ 0 0.238923 0.501682 0.259952 0.007046 0.794067 0.392352 0.296982 1 0.867870 0.861766 0.538408 0.277828 0.418462 0.528816 0.006187 28 29 30 31 32 33 34 \ 0 0.242004 0.647488 0.406277 0.239739 0.086527 0.661979 0.703148 1 0.184497 0.260851 0.599812 0.064739 0.736251 0.805404 0.899075 35 36 37 38 39 0 0.592287 0.350457 0.372377 0.246324 0.342693 1 0.785853 0.289017 0.504370 0.770860 0.416821
As observed above, the output now shows all the columns from the pandas DataFrame. We can set this value to “None” in case we want to always display all the columns in any DataFrame. Like,
# change the max_columns value to infinite pd.set_option("display.max_columns", None) print (df)
It will display all columns of the DataFrame while printing.
Expand display to show all columns using pandas.options() method
Instead of using it as an argument, we can directly change the max_columns value using the options function from Pandas. Let’s again try to expand the columns to 40.
# change the max_columns value to 40 pd.options.display.max_columns = 40 print (df)
Let’s checkout the complete example,
# importing library import numpy as np import pandas as pd # random DataFrame df = pd.DataFrame(np.random.random(80).reshape(2, 40)) # change the max_columns value to 40 pd.options.display.max_columns = 40 print (df)
Output
0 1 2 3 4 5 6 \ 0 0.099547 0.317654 0.696267 0.500405 0.662395 0.347217 0.919962 1 0.183956 0.093377 0.914585 0.824104 0.178390 0.714917 0.740702 7 8 9 10 11 12 13 \ 0 0.792616 0.856906 0.470693 0.828003 0.167716 0.705077 0.900042 1 0.099741 0.748058 0.281502 0.632023 0.428459 0.123901 0.231481 14 15 16 17 18 19 20 \ 0 0.753212 0.838928 0.762214 0.256567 0.874984 0.396890 0.600915 1 0.649959 0.205741 0.174359 0.349932 0.758043 0.092794 0.180718 21 22 23 24 25 26 27 \ 0 0.238923 0.501682 0.259952 0.007046 0.794067 0.392352 0.296982 1 0.867870 0.861766 0.538408 0.277828 0.418462 0.528816 0.006187 28 29 30 31 32 33 34 \ 0 0.242004 0.647488 0.406277 0.239739 0.086527 0.661979 0.703148 1 0.184497 0.260851 0.599812 0.064739 0.736251 0.805404 0.899075 35 36 37 38 39 0 0.592287 0.350457 0.372377 0.246324 0.342693 1 0.785853 0.289017 0.504370 0.770860 0.416821
Both the above methods are quite similar. Although, in cases where we need to set multiple values at once, the first method is cleaner as it allows us to set everything in one-liner code.
pd.set_option("display.max_columns", 40, "display.max_rows", 50)
For example, above we have set both the max_columns and max_rows together. You can also use None instead of any integer value, in that case it will show all rows and columns.
Expand display to show all columns using pandas.option_context() function
Note that both the above methods change these default values at the global level, meaning, post that the changes would be reflected in all the display commands. But in case, where we just want to change these settings for a single run, the option_context() function from pandas comes in handy.
# using option_context with pd.option_context('display.max_columns', None): print (df)
Inside thw with block, if we print DataFrame, then it will print all columns. Let’s see the complete example,
# importing library import numpy as np import pandas as pd # random DataFrame df = pd.DataFrame(np.random.random(80).reshape(2, 40)) # using option_context with pd.option_context('display.max_columns', None): print (df)
Output
0 1 2 3 4 5 6 \ 0 0.632569 0.563294 0.473282 0.625990 0.88394 0.969119 0.571010 1 0.790676 0.958089 0.908703 0.553424 0.89150 0.562219 0.407857 7 8 9 10 11 12 13 \ 0 0.379490 0.428773 0.664126 0.263777 0.518166 0.195164 0.222407 1 0.206793 0.773425 0.542391 0.892428 0.934983 0.053018 0.735291 14 15 16 17 18 19 20 \ 0 0.275035 0.282219 0.138600 0.509403 0.121989 0.977795 0.950575 1 0.794428 0.758629 0.210019 0.625313 0.201312 0.323290 0.281961 21 22 23 24 25 26 27 \ 0 0.179377 0.663175 0.602607 0.409337 0.170292 0.933000 0.876569 1 0.819548 0.909461 0.709151 0.575640 0.208872 0.788984 0.119483 28 29 30 31 32 33 34 \ 0 0.235779 0.736269 0.546753 0.742913 0.338967 0.652793 0.841542 1 0.666341 0.442340 0.302817 0.594159 0.975746 0.303037 0.093311 35 36 37 38 39 0 0.250550 0.761866 0.140867 0.522595 0.936228 1 0.140261 0.623021 0.488438 0.880718 0.149635
In case, we print any other DataFrame outside the with block, it will continue using the default settings.
Summary
Great, you made it! In this article, we have discussed multiple ways to the output display to see more columns of a Pandas DataFrame.