TravisVOX TravisVOX - 1 year ago 111
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
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 Source

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


list(df.index.values)  # this will always work in pandas
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download