Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get row number in dataframe in Pandas?

Tags:

python

pandas

How can I get the number of the row in a dataframe that contains a certain value in a certain column using Pandas? For example, I have the following dataframe:

     ClientID  LastName 0    34        Johnson 1    67        Smith 2    53        Brows   

How can I find the number of the row that has 'Smith' in 'LastName' column?

like image 424
sprogissd Avatar asked Apr 03 '17 20:04

sprogissd


People also ask

How do I get a row in Panda DataFrame?

To get the nth row in a Pandas DataFrame, we can use the iloc() method. For example, df. iloc[4] will return the 5th row because row numbers start from 0.

How do I add a row number to a DataFrame?

To Generate Row number to the dataframe in R we will be using seq.int() function. Seq.int() function along with nrow() is used to generate row number to the dataframe in R. We can also use row_number() function to generate row index.

Which function is used to get the number of rows in a DataFrame?

len() method is used to get the number of rows and number of columns individually.


2 Answers

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.

like image 132
joelostblom Avatar answered Sep 30 '22 12:09

joelostblom


df.index[df.LastName == 'Smith'] 

Or

df.query('LastName == "Smith"').index 

Will return all row indices where LastName is Smith

Int64Index([1], dtype='int64') 
like image 45
piRSquared Avatar answered Sep 30 '22 14:09

piRSquared