How to Find the Minimum Value by Group in Pandas


You can use the following methods to find the minimum value by group in a pandas DataFrame:

Method 1: Groupby minimum of one column

df.groupby('group_column')['values_column'].min()

Method 2: Groupby minimum of multiple columns

df.groupby('group_column')['values_column1', 'values_column2'].min()

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create pandas DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B', 'C', 'C'],
                   'points':[24, 23, 27, 11, 14, 8, 13],
                   'rebounds': [11, 8, 7, 6, 6, 5, 12]})

#display DataFrame
print(df)

  team  points  rebounds
0    A      24        11
1    A      23         8
2    B      27         7
3    B      11         6
4    B      14         6
5    C       8         5
6    C      13        12

Example 1: Groupby Minimum of One Column

The following code shows how to find the minimum value of the points column, grouped by the team column:

#find minimum value of points, grouped by team
df.groupby('team')['points'].min() 

team
A    23
B    11
C     8
Name: points, dtype: int64

From the output we can see:

  • The minimum value of points for team A is 23.
  • The minimum value of points for team B is 11.
  • The minimum value of points for team C is 8.

Example 2: Groupby Minimum of Multiple Columns

The following code shows how to find the minimum value of the points and rebounds columns, grouped by the team column:

#find minimum value of points and rebounds, grouped by team
df.groupby('team')[['points', 'rebounds']].min() 

    points  rebounds
team		
A	23	   8
B	11	   6
C	8	   5

From the output we can see:

Team A:

  • Minimum points: 23
  • Minimum rebounds: 8

Team B:

  • Minimum points: 11
  • Minimum rebounds: 6

Team C:

  • Minimum points: 8
  • Minimum rebounds: 5

Note: It’s important that you use double brackets when specifying the value columns, otherwise you may receive an error.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Calculate the Sum of Columns in Pandas
How to Calculate the Mean of Columns in Pandas
How to Find the Max Value of Columns in Pandas

Leave a Reply

Your email address will not be published.