[python] How to show all of columns name on pandas dataframe?

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?

This question is related to python pandas dataframe show

The answer is


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

data_all2.columns.tolist()

Or this within a script:

print(data_all2.columns.tolist())

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.


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


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]

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.


you can try this

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

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


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)

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.


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.


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.


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

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to pandas

xlrd.biffh.XLRDError: Excel xlsx file; not supported Pandas Merging 101 How to increase image size of pandas.DataFrame.plot in jupyter notebook? Trying to merge 2 dataframes but get ValueError Python Pandas User Warning: Sorting because non-concatenation axis is not aligned How to show all of columns name on pandas dataframe? Pandas/Python: Set value of one column based on value in another column Python Pandas - Find difference between two data frames Pandas get the most frequent values of a column Python convert object to float

Examples related to dataframe

Trying to merge 2 dataframes but get ValueError How to show all of columns name on pandas dataframe? Python Pandas - Find difference between two data frames Pandas get the most frequent values of a column Display all dataframe columns in a Jupyter Python Notebook How to convert column with string type to int form in pyspark data frame? Display/Print one column from a DataFrame of Series in Pandas Binning column with python pandas Selection with .loc in python Set value to an entire column of a pandas dataframe

Examples related to show

How to show all of columns name on pandas dataframe? jQuery select change show/hide div event Hide Show content-list with only CSS, no javascript used How can I hide/show a div when a button is clicked? jQuery hide and show toggle div with plus and minus icon Show div #id on click with jQuery jQuery if statement to check visibility How to print a double with two decimals in Android? Programmatically Hide/Show Android Soft Keyboard Show Hide div if, if statement is true