# Pandas – Get Rows with Maximum and Minimum Column Values

In this tutorial, we will look at how to get the rows having the maximum and the minimum column values in a pandas dataframe with the help of some examples.

When working with data in a pandas dataframe, at times, you may need to get the row where a certain column has the maximum and/or the minimum values. You can use the pandas `loc[]` property along with the `idxmax()` and `idxmin()` functions to get the row with maximum and minimum column values.

The following is the syntax.

```# get the row with the max value in a given column
df.loc[df['Col_name'].idxmax()]
# get the row with the min value in a given column
df.loc[df['Col_name'].idxmin()]```

## Examples

Let’s now look at some examples of using the above syntax.

First, we will create a pandas dataframe that we will be using throughout this tutorial.

```import pandas as pd
# Student data
data = {
'Name':['Alisa','Bobby','Tushar','Swastik','Ram','Dibya','Mama','Supriya'],
'Age':[26,24,23,22,23,24,26,24],

'Score':[85,88,55,74,31,77,75,63]
}
# create pandas dataframe
df = pd.DataFrame(data)
# display the dataframe
df```

Output:

Here, we created a dataframe with information about students. The dataframe has the columns – “Name”, “Age”, and “Score”.

### Example 1: Select Rows with Maximum Column Values

The following code shows how to select the row in the above DataFrame where the ‘Score’ column has the maximum value i.e. get all the details of the student with the maximum score:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

```# get the row where "Score" is maximum
df.loc[df['Score'].idxmax()]```

Output:

```Name     Bobby
Age         24
Score       88
Name: 1, dtype: object```

You can see we get the row where “Score” is maximum. `df['Score'].idxmax()` returns the index of the row where the column “Score” has the maximum value and `df.loc[]` gives the row corresponding to that index.

### Example 2: Select Rows with Minimum Column Values

You can similarly get the row in the above dataframe where the “Score” column has the minimum value.

```# get the row where "Score" is minimum
df.loc[df['Score'].idxmin()]```

Output:

```Name     Ram
Age       23
Score     31
Name: 4, dtype: object```

You can see we get the row where “Score” is minimum. `df['Score'].idxmin()` returns the index of the row where the column “Score” has the minimum value and `df.loc[]` returns the row of that index.

## Summary

In this tutorial, we looked at how to get rows with the maximum and the minimum values in a column in pandas with the help of some examples.

• You can get the row of the column maximum in pandas by using `df.loc[df['Col_name'].idxmax()]`. Here, `df['Col_name'].idxmax()` returns the index of the row where the column has the maximum value and `df.loc[]` returns the row for that index.
• You can get the row of the column minimum in pandas by using `df.loc[df['Col_name'].idxmin()]`. Here, `df['Col_name'].idxmin()` returns the index of the row where the column has the minimum value and `df.loc[]` returns the row for that index.

You might also be interested in –