How to get index of rows in Pandas DataFrame

Through this Python tutorial, we will go through the different methods of how to get index of rows in Pandas DataFrame. And I will show a few examples of how to get the row indexes of DataFrames in Python.

In this Python tutorial, we will be covering the topics related to getting the index of rows of Pandas DataFrames like:

  • How to get the index of rows based on column value in Pandas DataFrame
  • How to get the index of rows with multiple conditions in Pandas DataFrame
  • How to get a row index of columns with maximum value in Pandas DataFrame
  • How to get a row index of columns with minimum value in Pandas DataFrame

Get the index of rows in Pandas DataFrame

Row Indexes are also known as DataFrame Indexes. We can extract the index of the rows of Pandas DataFrame in Python using the existing and most widely used functions like “DataFrame.index”. To do that we need to create a DataFrame using Pandas.

Create a DataFrame using Pandas in Python :

Let us create our own Pandas DataFrame with multiple rows so that we can extract the indexes of a DataFrame using the “DataFrame.Index” property in Python.

Here We have created a dictionary of patients’ data that has the names of the patients, their ages, and the diseases from which they are suffering.

And later it is passed to the “pandas.DataFrame” function in order to convert it to a DataFrame or a table i.e in the form of rows and columns.

# Import necessary libraries
import numpy as np
import pandas as pd

#Create a dictionary which has names of the patients, their ages, and the diseases 
data_dict={"Patient":["Kelvin", "John", "smith", "Robin","Williams","Nick","Anyy","Messi","Jonas","Xavier"],
"Age":[13,71,67,8,56,12,31,3,np.nan,17],
"Diesease":["Acidity","Heart Attack","Cancer","Cancer","Heart Attack","Brain Stroke","Acidity","Heart Attack","Brain Stroke","Skin Cancer"],
}

#Create a DataFrame using Pandas
Patients_data=pd.DataFrame(data_dict)
Patients_data

If We look at the below image, a new DataFrame “Patients_data” is created and Jonas’s age is NaN which means Not a Number or None in Python.

Create a pandas dataframe in python
Create Pandas DataFrame in Python

Here, we have created a DataFrame to perform further analysis that contains the Patients’ data.

Now let us check different methods for the row indexes of Pandas DataFrames.

How to get index of rows based on column value in Pandas DataFrame

We can get the indexes of the rows based on the column value too. Suppose we want to know the indexes of the patients who are suffering from a heart attack from the above pandas DataFrame.

Here we are trying to extract the indexes of rows based on column values. In this example, we extracted the row indexes of patients who are actually suffering from heart attacks by filtering the patients with a heart attack first and then finding the indexes.

# Get index of rows based on column values in a pandas dataframe
print(Patients_data[Patients_data['Diesease']=="Heart Attack"].index)
  • The above Python code “Patients_data[Patients_data[‘Diesease’]==”Heart Attack”] ” returns the subset of the patient DataFrame.
  • And this subset DataFrame has only patients who are suffering from a heart attack.
  • The “.index” operator returns the indexes of the above subset pandas DataFrame in python
  • The output [1,4,7] indicates that patients who are at these indexes are suffering from Heart Attacks.
Get row indexes of pandas dataframe based on column value in python
Get row indexes of pandas dataframe based on column value in Python

This is all about getting the index of rows based on column value in Pandas DataFrame.

How to get index of rows with multiple conditions in Pandas DataFrame

We can even extract the row indexes of a Pandas DataFrame by applying multiple conditions I.e, bypassing multiple conditions within the DataFrame in Python.

In the above example, we filtered the row indexes of a Pandas DataFrame based on a single column value that is disease==”Heart Attack”.

But here We are getting the indexes of rows by applying multiple conditions I.e here we are trying to extract indexes of patients who are suffering from heart attack and with an age limit of above 50 in Python.

#Get index of rows by applying multiple conditions to a pandas dataframe in python
Patients_data[(Patients_data['Diesease']=="Heart Attack") &(Patients_data['Age']>50)].index
  • The above python code “Patients_data[(Patients_data[‘Diesease’]==”Heart Attack”) &(Patients_data[‘Age’]>50)] ” returns the subset of the patient DataFrame.
  • And this subset dataframe has only patients who are suffering from a heart attack with ages above 50 years.
  • The “.index” operator returns the indexes of the above subset Pandas DataFrame in Python
  • The output [1,4] indicates that patients who are at these indexes are suffering from Heart Attacks and their age is above 50 years.
Get index of rows of a pandas dataframe in python
Get indexes of rows of Pandas DataFrame with multiple conditions

This is all about getting the index of rows with multiple conditions in Pandas DataFrame.

How to get row index of columns with maximum value in Pandas DataFrame

There is an inbuilt function called “idxmax()” in Python which will return the indexes of the rows in the Pandas DataFrame by filtering the maximum value from each column.

It will display the row index for every numeric column that has the maximum value.

Through the below code, we are trying to find the index of the row that has the maximum value in the column “Age”.

# Get a row index of a patient with maximum age
Patients_data['Age'].idxmax()

From the below output image, we can understand the patient who is at index position “1” has more age i.e about 71 years old among all the patients in the pandas dataframe that is created.

get row index of a pandas dataframe using idxmax() in python
Get the index of patients with
maximum age in the data

This is all about getting the row index of columns with maximum value in Pandas DataFrame.

How to get row index of columns with minimum value in Pandas DataFrame

There is an inbuilt function called “idxmin()” in Pandas in Python which will return the indexes of the rows in the Pandas DataFrame by filtering the minimum value from each column.

It will display the row index for every numeric column that has the minimum value.

Through the below code, we are trying to find the index of the row that has the minimum value in the column “Age”.

# Get a row index of a patient with minimum age
Patients_data['Age'].idxmin()

From the below output image, we can understand the patient who is at index position “7” has a very less age i.e about 3 years among all the patients in the pandas dataframe that is created.

get the row index of a pandas dataframe using idxmin in python
Get the index of patients with minimum age in the data

This is all about getting the row index of columns with minimum value in Pandas DataFrame.

Conclusion

Through this Python Pandas tutorial, we have covered topics related to getting the indexes of rows in Pandas DataFrame based on different conditions like:

  • How to get the index of rows based on column value in Pandas DataFrame
  • How to get the index of rows with multiple conditions in Pandas DataFrame
  • How to get a row index of columns with maximum value in Pandas DataFrame
  • How to get a row index of columns with minimum value in Pandas DataFrame

We covered all these topics along with examples to make our learning journey easier. Also, you can check the below Python Pandas tutorials.