Get row-index values of Pandas DataFrame as list?

TravisVOX picture TravisVOX · Aug 21, 2013 · Viewed 190.6k times · Source

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

Phillip Cloud picture Phillip Cloud · Aug 21, 2013

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