How to show all of columns name on pandas dataframe?

南笙酒味 提交于 2020-05-09 17:38:29

问题


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?


回答1:


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 want to see just the column names, you can do: cols = df.columns.tolist()




回答2:


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



回答3:


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

data_all2.columns.tolist()

Or this within a script:

print(data_all2.columns.tolist())



回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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]



回答8:


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




回答9:


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




回答10:


you can try this

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



回答11:


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.




回答12:


This is my way. I never try for hundred columns. But I think it works

your_dataframe.info()



回答13:


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)


来源:https://stackoverflow.com/questions/49188960/how-to-show-all-of-columns-name-on-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!