The Python Oracle

How to show all columns' names on a large pandas dataframe?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC G Dvoks String Quartet No 12 Ame 2

--

Chapters
00:00 Question
00:46 Accepted answer (Score 514)
01:25 Answer 2 (Score 70)
01:55 Answer 3 (Score 31)
02:20 Answer 4 (Score 17)
02:37 Thank you

--

Full question
https://stackoverflow.com/questions/4918...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #pandas #dataframe

#avk47



ACCEPTED ANSWER

Score 650


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())



ANSWER 2

Score 80


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']



ANSWER 3

Score 47


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.




ANSWER 4

Score 18


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

data_all2.columns.tolist()

Or this within a script:

print(data_all2.columns.tolist())