user6162407 user6162407 - 6 months ago 18
Python Question

Access the row number by its index

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.

Answer

You can also use get_loc:

df.index.get_loc(index_value)
Out[67]: 1