从pandas DataFrame列标题获取列表

本文翻译自: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)
发布了0 篇原创文章 · 获赞 73 · 访问量 55万+

猜你喜欢

转载自blog.csdn.net/w36680130/article/details/105328745
今日推荐