Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the name of the rows from the index of a data frame?

Consider a data frame with row names that aren't a column of their own per se, such as the following:

        X  Y  Row 1  0  5  Row 2  8  1  Row 3  3  0 

How would I extract the name of these rows as a list, if I have their index? For example, it would look something like:

function_name(dataframe[indices]) > ['Row 1', 'Row 2'] 
like image 886
linamnt Avatar asked Oct 29 '14 20:10

linamnt


People also ask

How do I find the row name in a DataFrame?

Get Row Index Label Names from a DataFrame object listOfRowIndexLabels is a list that contains all the row index labels of a DataFrame object i.e. As df. index. values is a ndarray, so we can access it contents by position too.

How do I select a row by index name?

You can select a single row from pandas DataFrame by integer index using df. iloc[n] . Replace n with a position you wanted to select.


2 Answers

df.index

  • outputs the row names as pandas Index object.

list(df.index)

  • casts to a list.

df.index['Row 2':'Row 5']

  • supports label slicing similar to columns.
like image 199
Adam Hughes Avatar answered Sep 19 '22 23:09

Adam Hughes


this seems to work fine :

dataframe.axes[0].tolist() 
like image 42
Alex Kushkuley Avatar answered Sep 20 '22 23:09

Alex Kushkuley