Python 如何在 Pandas 数据框上显示所有列名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49188960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to show all of columns name on pandas dataframe?
提问by Nabih Bawazir
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 allcolumns, instead of a truncated list?
如何显示所有列,而不是截断的列表?
回答by YOLO
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.
这将允许您在执行.head()
. 不会截断任何列名称。
If you want to see just the column names, you can do:
cols = df.columns.tolist()
如果只想查看列名,可以执行以下操作:
cols = df.columns.tolist()
回答by pink.slash
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
获取一个DataFrame的所有列名,df_data
在本例中,只需要使用命令即可df_data.columns.values
。这将显示一个包含数据框所有列名称的列表
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']
回答by EEE
In the interactive console, it's easy to do:
在交互式控制台中,很容易做到:
data_all2.columns.tolist()
Or this within a script:
或者在脚本中这样:
print(data_all2.columns.tolist())
回答by S. Tibbitts
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.
您还可以将其设置为大于列数的整数。
回答by nico
This will do the trick. Note the use of display()
instead of print.
这将解决问题。注意使用display()
而不是打印。
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
。
回答by Ashwani Shakya
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.
您将获得所有列名称。或者您可以将所有列名存储到另一个列表变量,然后打印列表。
回答by naimur978
you can try this
你可以试试这个
pd.pandas.set_option('display.max_columns', None)
回答by Aman
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)
查看所有内容。
回答by R.K.
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
我能够看到完整的列列表
回答by Rao Sahab
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 将作为一个可以被索引的迭代变量。例如
cols[11:20]