240

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?

smci
  • 29,564
  • 18
  • 109
  • 144
Nabih Bawazir
  • 5,275
  • 6
  • 29
  • 53

16 Answers16

422

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,542
  • 3
  • 19
  • 26
YOLO
  • 18,072
  • 3
  • 18
  • 39
  • 4
    @EEE No, it does answer the question. I just tried it, it displays all columns instead of a truncated list. He didn't say truncated field, he said column list. – rjurney Feb 12 '19 at 00:13
  • 3
    Ahh, you're right. I was wrong. Thanks, @rjurney. And sorry YOLO. I was doing df.columns instead of df.head()! Should I delete my incorrect earlier comment? – EEE Feb 14 '19 at 20:32
  • Yeah, I think so. – rjurney Feb 15 '19 at 22:07
  • 2
    it's much better to set a finite value like 500 otherwise it will take forever to run if you print a big dataframe – Tomas G. Apr 06 '20 at 06:14
  • 2
    Sorry, but how to turn it off? Currently my dumb solution is simply to close Anaconda and reopen it. Well, is there a legit to get out it without doing so? – Chen Lizi Jul 30 '20 at 21:45
  • Setting these options was not enough for me. I also needed `display.max_seq_items`. – Michel de Ruiter Nov 10 '20 at 17:17
  • it helps to know that you can expand your output width (if you got too many columns to show in one line): pd.options.display.width = 1000 – salouri Jan 26 '21 at 15:22
  • 1
    @ChenLizi To reset it there is `pd.reset_option("display.max_rows")` –  Feb 14 '22 at 10:03
66

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,647
  • 14
  • 13
  • This is the real answer to this question, thank you @pink.slash – Interlooper Apr 26 '19 at 15:41
  • If I'd like to see the column numbers as well - is that possible? My df has 200 columns and I'd like to use a fraction of them, and was thinking I could use the numbers instead of writing each column name . – Mactilda Oct 23 '19 at 08:25
  • hmm, it does not work on my system, it shows even fewer than df_data.columns... – Xin Niu Sep 09 '21 at 08:19
18

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
  • 936
  • 9
  • 32
  • I like the `with` keyword to apply the option only to the block below. However it works well with `print()`. Why would I need to use `display()` instead of `print()`? – Vincent Agami Jan 28 '20 at 09:54
  • @VincentAgami The use of display is required because pd.option_context settings only apply to display and not to print. I have updated the answer to include this info. – nico Apr 11 '20 at 13:33
  • That's not true, the settings work fine with `print()`. Furthermore, `display()` is a function provided by IPython, which not everyone might use. – cbrnr May 27 '22 at 11:24
16

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

data_all2.columns.tolist()

Or this within a script:

print(data_all2.columns.tolist())
EEE
  • 466
  • 6
  • 13
7

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
  • 71
  • 1
  • 3
7

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
  • 107
  • 2
  • 5
  • 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
4

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
  • 101
  • 8
  • _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
3

you can try this

pd.pandas.set_option('display.max_columns', None)
naimur978
  • 109
  • 6
3

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)
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
  • 431
  • 2
  • 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
  • 401
  • 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.
  • 2,911
  • 19
  • 23
2

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
  • 988
  • 5
  • 11
  • 21
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
  • 913
  • 1
  • 9
  • 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
  • 18,131
  • 10
  • 32
  • 49