Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access the row number by its index

Tags:

python

pandas

I'm trying to access the row number with an index value corresponding to that row.

mydata = [{'name': 'John', 'age': 75, 'height':1.78},
      {'name': 'Paul', 'age': 22, 'height':1.71}]

df = pandas.DataFrame(mydata)
df = df.set_index('name')

Get index value of row number 1

index_value = df.index[1]

With having that index value, how do I go about returning a row number? In this case, 1.

like image 756
user6162407 Avatar asked Mar 12 '23 04:03

user6162407


1 Answers

You can also use get_loc:

df.index.get_loc(index_value)
Out[67]: 1
like image 117
ayhan Avatar answered Mar 14 '23 17:03

ayhan