Get row-index values of Pandas DataFrame as list?
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.
To get the index
values as a list
/list
of tuple
s 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