简体   繁体   English

如何在大型 pandas dataframe 上显示所有列的名称?

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

I have a dataframe that consist of hundreds of columns, and I need to see all column names.我有一个包含数百列的 dataframe,我需要查看所有列名。

What I did:我做了什么:

In[37]:
data_all2.columns

The output is: output 是:

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?如何显示所有列,而不是截断列表?

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() .这将允许您在执行.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())

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 .要获取 DataFrame 的所有列名,在本例中为df_data ,您只需要使用命令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']

This will do the trick.这会成功的。 Note the use of display() instead of print.注意使用display()而不是 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 .需要使用display ,因为pd.option_context设置仅适用于display而不是print

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.您还可以将其设置为大于列数的整数。

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.就我个人而言,我不想更改全局变量,我不想经常看到所有列名。

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.如果行仍然被截断,只需使用print(data_all2.T)查看所有内容。

你可以试试这个

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

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 :要显示所有列名而不换行,请同时设置 display.max_columns 和display.width

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

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.show_counts设置为True会显示not_null数据的计数。

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会导致所有这些都被打印出来,由制表符分隔当然,请注意,有 102 个名称,它们都相当长,这将有点难以阅读

To get all column name you can iterate over the data_all2.columns .要获取所有列名,您可以遍历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.或者您可以将所有列名存储到另一个列表变量,然后打印列表。

I know it is a repetition but I always end up copy pasting and modifying YOLO's answer:我知道这是重复但我总是复制粘贴和修改 YOLO 的答案:

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

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.现在 cols 将表现为一个可以索引的迭代变量。 for example例如

cols[11:20]

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信用: https ://stackoverflow.com/a/40435354/5846417

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:这是我在 Spyder 中使用的:

print(df.info()) 

or this be what is needed in Jupyter:或者这就是 Jupyter 中需要的:

df.info()

“df.types”将数据框“df”的所有列作为行输出,作为附带奖励,您还将获得数据类型。

Try this one -试试这个——

df.columns.values df.columns.values

df.head(None)

In this way, you can see all things with a format data frame.通过这种方式,您可以使用格式数据框查看所有内容。 You need to write你需要写

data_all2.head(None)

My go-to function to print every column on my console is:我在控制台上打印每一列的首选 function 是:

pandas.set_option('display.expand_frame_repr', False)

for i in df2.columns.tolist(): print(i)对于 df2.columns.tolist() 中的 i: print(i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM