Show All Columns for Large pandas DataFrame
By default, pandas display only 10 columns (first and last 5 columns and truncate middle section) for large DataFrame.
However, you can use the set_option
function from pandas to set the maximum columns to display for a large DataFrame.
The basic syntax for the set_option
function to display all columns is:
# import package
import pandas as pd
pd.set_option('display.max_columns', None)
If you use None
, it will display all columns in the pandas DataFrame.
The following example explains how to use the pandas set_option
function for displaying all columns of a large DataFrame.
Create a random pandas DataFrame with 100 columns,
# load packages
import pandas as pd
import numpy as np
# crate random pandas dataframe
df = pd.DataFrame(np.random.rand(100, 100),
columns=[f'col_{i}' for i in range(100)])
# view DataFrame
df
# output
col_0 col_1 col_2 col_3 col_4 ... col_95 col_96 col_97 col_98 col_99
0 0.211046 0.178511 0.684030 0.651491 0.574638 ... 0.602286 0.182692 0.184578 0.783476 0.242175
1 0.905484 0.359048 0.282908 0.513805 0.088649 ... 0.808148 0.477627 0.642036 0.213199 0.711586
2 0.335208 0.983278 0.306159 0.796319 0.074314 ... 0.566329 0.067373 0.724745 0.441022 0.039970
3 0.375109 0.727431 0.729371 0.684975 0.692459 ... 0.835979 0.703979 0.999638 0.127669 0.768954
4 0.500143 0.083834 0.637238 0.899191 0.003734 ... 0.471217 0.709752 0.955857 0.918538 0.361354
.. ... ... ... ... ... ... ... ... ... ... ...
95 0.544771 0.175157 0.814886 0.519858 0.345644 ... 0.745995 0.278358 0.807579 0.986876 0.528559
96 0.241740 0.489561 0.910873 0.931028 0.674234 ... 0.817299 0.033795 0.082097 0.305525 0.362735
97 0.316736 0.357365 0.500066 0.376462 0.078107 ... 0.073056 0.885287 0.060763 0.328637 0.374452
98 0.430606 0.599714 0.061552 0.578797 0.559666 ... 0.054318 0.886145 0.271992 0.411100 0.854295
99 0.327226 0.570996 0.332724 0.115161 0.146080 ... 0.552738 0.728426 0.222166 0.323721 0.994960
You can see that by default pandas DataFrame displays the first and last 5 columns and truncates the middle section.
If you want to see all 100 columns, you can use the set_option
function from pandas.
# display 100 columns
pd.set_option('display.max_columns', None)
# check number of columns to display
print(pd.options.display.max_columns)
# output
None
# view DataFrame
df
# output
# it will display all columns (not shown here)
You can also reset the display of max number of columns to default using pd.reset_option('display.max_columns')
code.
# reset to default (10 columns)
pd.reset_option('display.max_columns')
If you just want to display all column names, you can use the following code:
# display all column names
df.columns.values
# output
array(['col_0', 'col_1', 'col_2', 'col_3', 'col_4', 'col_5', 'col_6',
'col_7', 'col_8', 'col_9', 'col_10', 'col_11', 'col_12', 'col_13',
'col_14', 'col_15', 'col_16', 'col_17', 'col_18', 'col_19',
'col_20', 'col_21', 'col_22', 'col_23', 'col_24', 'col_25',
'col_26', 'col_27', 'col_28', 'col_29', 'col_30', 'col_31',
'col_32', 'col_33', 'col_34', 'col_35', 'col_36', 'col_37',
'col_38', 'col_39', 'col_40', 'col_41', 'col_42', 'col_43',
'col_44', 'col_45', 'col_46', 'col_47', 'col_48', 'col_49',
'col_50', 'col_51', 'col_52', 'col_53', 'col_54', 'col_55',
'col_56', 'col_57', 'col_58', 'col_59', 'col_60', 'col_61',
'col_62', 'col_63', 'col_64', 'col_65', 'col_66', 'col_67',
'col_68', 'col_69', 'col_70', 'col_71', 'col_72', 'col_73',
'col_74', 'col_75', 'col_76', 'col_77', 'col_78', 'col_79',
'col_80', 'col_81', 'col_82', 'col_83', 'col_84', 'col_85',
'col_86', 'col_87', 'col_88', 'col_89', 'col_90', 'col_91',
'col_92', 'col_93', 'col_94', 'col_95', 'col_96', 'col_97',
'col_98', 'col_99'], dtype=object)