307

I have a dataframe that consist of hundreds of columns, and I need to see all column names.

What I did:

In[37]:
data_all2.columns

The output is:

Out[37]:
Index(['customer_id', 'incoming', 'outgoing', 'awan', 'bank', 'family', 'food',
       'government', 'internet', 'isipulsa',
       ...
       'overdue_3months_feature78', 'overdue_3months_feature79',
       'overdue_3months_feature80', 'overdue_3months_feature81',
       'overdue_3months_feature82', 'overdue_3months_feature83',
       'overdue_3months_feature84', 'overdue_3months_feature85',
       'overdue_3months_feature86', 'loan_overdue_3months_total_y'],
      dtype='object', length=102)

How do I show all columns, instead of a truncated list?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Nabih Bawazir
  • 6,381
  • 7
  • 37
  • 70
  • 1
    This is what I often use: `import pandas as pd; pd.set_option('display.max_rows', 1000); pd.set_option('display.max_columns', 1000); pd.set_option('display.width', 1000)`. The benefit is that *not only* all colums are well displayed, but also the printed rows can be larger than the usual ~100 characters limit. – Basj Aug 19 '22 at 18:00

22 Answers22

562

You can globally set printing options. I think this should work:

Method 1:

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

Method 2:

pd.options.display.max_columns = None
pd.options.display.max_rows = None

This will allow you to see all column names & rows when you are doing .head(). None of the column name will be truncated.


If you just want to see the column names you can do:

print(df.columns.tolist())
young_souvlaki
  • 1,886
  • 4
  • 24
  • 28
YOLO
  • 20,181
  • 5
  • 20
  • 40
  • Method 2 worked where Method 1 didn't. Might be related to the answer below about context. posted by nico – kpierce8 May 16 '23 at 00:02
  • 1
    If you want to prevent wrapping of the output then you need to set the `display.width` option to a very large number. For example: `pd.set_option('display.width', 9999)` – BenVida Jul 10 '23 at 18:55
70

To obtain all the column names of a DataFrame, df_data in this example, you just need to use the command df_data.columns.values. This will show you a list with all the Column names of your Dataframe

Code:

df_data=pd.read_csv('../input/data.csv')
print(df_data.columns.values)

Output:

['PassengerId' 'Survived' 'Pclass' 'Name' 'Sex' 'Age' 'SibSp' 'Parch' 'Ticket' 'Fare' 'Cabin' 'Embarked']
pink.slash
  • 1,817
  • 15
  • 14
35

This will do the trick. Note the use of display() instead of print.

with pd.option_context('display.max_rows', 5, 'display.max_columns', None): 
    display(my_df)

EDIT:

The use of display is required because pd.option_context settings only apply to display and not to print.

nico
  • 1,136
  • 1
  • 15
  • 33
18

In the interactive console, it's easy to do:

data_all2.columns.tolist()

Or this within a script:

print(data_all2.columns.tolist())
EEE
  • 501
  • 6
  • 13
15

The accepted answer caused my column names to wrap around. To show all the column names without wrapping, set both display.max_columns and the display.width:

pandas.set_option('display.max_columns', None)
pandas.set_option('display.width', 1000)
9

What worked for me was the following:

pd.options.display.max_seq_items = None

You can also set it to an integer larger than your number of columns.

S. Tibbitts
  • 91
  • 1
  • 3
8

The easiest way I've found is just

list(df.columns)

Personally I wouldn't want to change the globals, it's not that often I want to see all the columns names.

Sherman
  • 437
  • 6
  • 9
  • This is why you can use a context manager so that you can limit the scope. – Giorgos Myrianthous Jul 13 '21 at 11:23
  • When you limit the scope you can run into strange issues. I found `option_context` works while `set_option` doesn't. They have their uses in many cases but for something this rudimentary I prefer a simple, easy to remember solution. – Sherman Apr 27 '22 at 19:59
6

If you want to see the all columns in Pandas df.head(), then use this snippet before running your code. All column data will be visible.

pd.pandas.set_option('display.max_columns', None)

After this create your dataframe, and try this.

df.head()

It will print the all columns instead of showing "...." in larger dataset.

Kushal Bhavsar
  • 388
  • 3
  • 6
5

Not a conventional answer, but I guess you could transpose the dataframe to look at the rows instead of the columns. I use this because I find looking at rows more 'intuitional' than looking at columns:

data_all2.T

This should let you view all the rows. This action is not permanent, it just lets you view the transposed version of the dataframe.

If the rows are still truncated, just use print(data_all2.T) to view everything.

Aman
  • 113
  • 2
  • 10
  • _Not a conventional answer, but I guess you could transpose the dataframe to look at the rows instead of the columns._ All they want is the column names, no? _If the rows are still truncated, just use print(data_all2.T) to view everything._ How would printing the result solve the issue? Are you not already printing it anyway? – AMC Apr 17 '20 at 23:02
4

you can try this

pd.pandas.set_option('display.max_columns', None)
naimur978
  • 144
  • 8
3

You can do like this

df.info(show_counts=True)

It will show all the columns. Setting show_counts to True shows the count of not_null data.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
3

Try this one -

df.columns.values

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 27 '22 at 19:08
  • This answer was already given [here](https://stackoverflow.com/a/54421030/18145256). – rachwa Jul 03 '22 at 08:20
2

A quick and dirty solution would be to convert it to a string

print('\t'.join(data_all2.columns))

would cause all of them to be printed out separated by tabs Of course, do note that with 102 names, all of them rather long, this will be a bit hard to read through

David L
  • 441
  • 3
  • 9
2

To get all column name you can iterate over the data_all2.columns.

columns = data_all2.columns
for col in columns:
    print col

You will get all column names. Or you can store all column names to another list variable and then print list.

Ashwani Shakya
  • 419
  • 4
  • 11
2

I know it is a repetition but I always end up copy pasting and modifying YOLO's answer:

pd.set_option('display.max_columns', 500)
pd.set_option('display.max_rows', 500)
Tomas G.
  • 3,784
  • 25
  • 28
2
df.head(None)

In this way, you can see all things with a format data frame. You need to write

data_all2.head(None)
2

My go-to function to print every column on my console is:

pandas.set_option('display.expand_frame_repr', False)
João
  • 301
  • 4
  • 9
2
for i in df2.columns.tolist():
  print(i)
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Roy Hwang
  • 21
  • 1
1

If you just want to see all the columns you can do something of this sort as a quick fix

cols = data_all2.columns

now cols will behave as a iterative variable that can be indexed. for example

cols[11:20]
Rao Sahab
  • 1,161
  • 1
  • 10
  • 12
1

I had lots of duplicate column names, and once I ran

df = df.loc[:,~df.columns.duplicated()]

I was able to see the full list of columns

Credit: https://stackoverflow.com/a/40435354/5846417

R.K.
  • 11
  • 3
1

I may be off the mark but I came to this thread with the same type of problem I found this is the simple answer if you want to see everything in a long list and the index.

This is what I use in Spyder:

print(df.info()) 

or this be what is needed in Jupyter:

df.info()
Syscall
  • 19,327
  • 10
  • 37
  • 52
1

"df.types" gets all the columns of data frame 'df' as output as rows, and as a side bonus, you will also get the data type.

StackOF
  • 11
  • 2
  • Your answer does not address the OP's question of how to display the **full** list of columns. With `df.dtypes` you will still get a truncated list, unless you take one of the steps described in the other answers. – AlexK Jun 10 '22 at 18:15