Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Data Science by (17.6k points)

So assume I have a dataframe with rownames 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 these row names as a list, if I have their index? For example, it would look something like:

function_name(dataframe[indices])

> ['Row 1', 'Row 2']

Thanks for your help!

1 Answer

0 votes
by (41.4k points)

If you want to pull out only the index values for certain integer-based row-indices, you can do something like the following

Use the iloc method to get only the index value for certain integer based row indices:

In [28]: temp

Out[28]:

       index                 time complete

row_0      2 2014-10-22 01:00:00         0

row_1      3 2014-10-23 14:00:00         0

row_2      4 2014-10-26 08:00:00         0

row_3      5 2014-10-26 10:00:00         0

row_4      6 2014-10-26 11:00:00         0

In [29]: temp.iloc[[0,1,4]].index

Out[29]: Index([u'row_0', u'row_1', u'row_4'], dtype='object')

In [30]: temp.iloc[[0,1,4]].index.tolist()

Out[30]: ['row_0', 'row_1', 'row_4']

If you want to learn more about Pandas then visit this Python Course Designed By Experts.

Browse Categories

...