Get list from pandas DataFrame column headers

I want to get a list of the column headers from a 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.

For example, if I'm given a DataFrame like this:

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

You can get the values as a list by doing:

list(my_dataframe.columns.values)

Also you can simply use:

list(my_dataframe)

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 to return a list .

EDIT

For those who hate typing this is probably the shortest method:

list(df)

Did some quick tests, and perhaps unsurprisingly the built-in version using dataframe.columns.values.tolist() is the fastest:

In [1]: %timeit [column for column in df]
1000 loops, best of 3: 81.6 µs per loop

In [2]: %timeit df.columns.values.tolist()
10000 loops, best of 3: 16.1 µs per loop

In [3]: %timeit list(df)
10000 loops, best of 3: 44.9 µs per loop

In [4]: % timeit list(df.columns.values)
10000 loops, best of 3: 38.4 µs per loop

(I still really like the list(dataframe) though, so thanks EdChum!)

链接地址: http://www.djcxy.com/p/12232.html

上一篇: 什么是这个阵列的最佳算法

下一篇: 从pandas DataFrame列标题中获取列表