Get list from pandas DataFrame column headers Get list from pandas DataFrame column headers python python

Get list from pandas DataFrame column headers


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):

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

If performance is not as important to you, Index objects define a .tolist() method that you can call directly:

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:

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 loopIn [2]: %timeit df.columns.values.tolist()10000 loops, best of 3: 16.1 µs per loopIn [3]: %timeit list(df)10000 loops, best of 3: 44.9 µs per loopIn [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!)


matomo