126

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?

smci
  • 32,567
  • 20
  • 113
  • 146
TravisVOX
  • 20,342
  • 13
  • 37
  • 41
  • 4
    *Why* do you want them as a list?? – Andy Hayden Aug 21 '13 at 13:40
  • If you're only getting these to manually pass into `df.set_index()`, that's unnecessary. Just directly do `df.set_index['your_col_name', drop=False]`, already. – smci Nov 17 '16 at 10:55
  • 1
    As for _why_... I personally need to get the index values of a dataframe as a list during debugging (Evaluate Expression in PyCharm) or double-checking between steps when programming interactively (for example, in a Jupyter notebook) all the time. – Attila Tanyi May 20 '17 at 13:31

2 Answers2

250

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
Phillip Cloud
  • 24,919
  • 11
  • 68
  • 88
2

If you're only getting these to manually pass into df.set_index(), that's unnecessary. Just directly do df.set_index['your_col_name', drop=False], already.

It's very rare in pandas that you need to get an index as a Python list (unless you're doing something pretty funky, or else passing them back to NumPy), so if you're doing this a lot, it's a code smell that you're doing something wrong.

smci
  • 32,567
  • 20
  • 113
  • 146