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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 anddf.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 anddf.loc[]
returns the row for that index.
You might also be interested in –
- Pandas – Get max value in one or more columns
- Get Maximum in each Group – Pandas Groupby
- Rolling Maximum in a Pandas Column
- Pandas – Get min value in one or more columns
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.