TravisVOX TravisVOX - 21 days ago 5
Python Question

Get index values of Pandas DataFrame as list?

I'm probably using poor search terms when trying to find this answer. Right now, before indexing a DataFrame, I'm getting a list of values in a column this way...

list = list(df['column'])


...then I'll
set_index
on the column. This seems like a wasted step. When trying the above on an index, I get a key error.

How can I grab the values in an index (both single and multi) and put them in a list or a list of tuples?

Answer

To get the index values as a list/list of tuples for Index/MultiIndex do:

df.index.values.tolist()  # an ndarray method, you probably shouldn't depend on this

or

list(df.index.values)  # this will always work in pandas