How to get row number in dataframe in Pandas?

Note that a dataframe’s index could be out of order, or not even numerical at all. If you don’t want to use the current index and instead renumber the rows sequentially, then you can use df.reset_index() together with the suggestions below

To get all indices that matches ‘Smith’

>>> df[df['LastName'] == 'Smith'].index
Int64Index([1], dtype="int64")

or as a numpy array

>>> df[df['LastName'] == 'Smith'].index.to_numpy()  # .values on older versions
array([1])

or if there is only one and you want the integer, you can subset

>>> df[df['LastName'] == 'Smith'].index[0]
1

You could use the same boolean expressions with .loc, but it is not needed unless you also want to select a certain column, which is redundant when you only want the row number/index.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)