概述
本文翻译自:Get list from pandas DataFrame column headers
I want to get a list of the column headers from a pandas DataFrame. 我想从pandas DataFrame获取列标题的列表。 The DataFrame will come from user input so I won't know how many columns there will be or what they will be called. DataFrame来自用户输入,所以我不知道会有多少列或它们将被称为什么。
For example, if I'm given a DataFrame like this: 例如,如果给我这样的DataFrame:
>>> my_dataframe
y gdp cap
0 1 2 5
1 2 3 9
2 8 7 2
3 3 4 7
4 6 7 7
5 4 8 3
6 8 2 8
7 9 9 10
8 6 6 4
9 10 10 7
I would want to get a list like this: 我想要一个这样的列表:
>>> header_list
['y', 'gdp', 'cap']
#1楼
参考:https://stackoom.com/question/1JkPS/从pandas-DataFrame列标题获取列表
#2楼
可以通过my_dataframe.columns
。
#3楼
You can get the values as a list by doing: 您可以执行以下操作以列表形式获取值:
list(my_dataframe.columns.values)
Also you can simply use: (as shown in Ed Chum's answer ): 您也可以简单地使用:(如Ed Chum的答案所示 ):
list(my_dataframe)
#4楼
n = []
for i in my_dataframe.columns:
n.append(i)
print n
#5楼
There is a built in method which is the most performant: 有一个内置的方法是最有效的:
my_dataframe.columns.values.tolist()
.columns
returns an Index, .columns.values
returns an array and this has a helper function .tolist
to return a list. .columns
返回一个索引, .columns.values
返回一个数组,它具有帮助函数.tolist
以返回列表。
If performance is not as important to you, Index
objects define a .tolist()
method that you can call directly: 如果性能对您不那么重要,则Index
对象定义一个.tolist()
方法,您可以直接调用该方法:
my_dataframe.columns.tolist()
The difference in performance is obvious: 性能差异很明显:
%timeit df.columns.tolist()
16.7 µs ± 317 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
%timeit df.columns.values.tolist()
1.24 µs ± 12.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
For those who hate typing, you can just call list
on df
, as so: 对于那些讨厌打字的人,您可以在df
上调用list
,如下所示:
list(df)
#6楼
A DataFrame follows the dict-like convention of iterating over the “keys” of the objects. DataFrame遵循类似dict的约定,即在对象的“键”上进行迭代。
my_dataframe.keys()
Create a list of keys/columns - object method to_list()
and pythonic way 创建键/列的列表-对象方法to_list()
和to_list()
方法
my_dataframe.keys().to_list()
list(my_dataframe.keys())
Basic iteration on a DataFrame returns column labels DataFrame的基本迭代返回列标签
[column for column in my_dataframe]
Do not convert a DataFrame into a list, just to get the column labels. 不要仅仅为了获取列标签而将DataFrame转换为列表。 Do not stop thinking while looking for convenient code samples. 寻找方便的代码示例时,请不要停止思考。
xlarge = pd.DataFrame(np.arange(100000000).reshape(10000,10000))
list(xlarge) #compute time and memory consumption depend on dataframe size - O(N)
list(xlarge.keys()) #constant time operation - O(1)
最后
以上就是震动铃铛为你收集整理的从pandas DataFrame列标题获取列表的全部内容,希望文章能够帮你解决从pandas DataFrame列标题获取列表所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复