Pandas – Get min value in one or more columns

Pandas dataframes are great for analyzing and manipulating data such as looking at descriptive stats like maximum and minimum. In this tutorial, we will look at how to get the min value in one or more columns of a pandas dataframe with the help of some examples.

Min value in each pandas column

You can use the pandas min() function to get the minimum value in a given column, multiple columns, or the entire dataframe. The following is the syntax:

# df is a pandas dataframe

# min value in a column
df['Col'].min()
# min value for multiple columns
df[['Col1', 'Col2']].min()
# min value for each numerical column in the dataframe
df.min(numeric_only=True)
# min value in the entire dataframe
df.min(numeric_only=True).min()

It returns the minimum value or values depending on the input and the axis (see the examples below).

Let’s look at some use-case of the pandas min() function. First, we’ll create a sample dataframe that we will be using throughout this tutorial.

import numpy as np
import pandas as pd

# create a pandas dataframe
df = pd.DataFrame({
    'Name': ['Neeraj Chopra', 'Jakub Vadlejch', 'Vitezslav Vesely', 'Julian Weber', 'Arshad Nadeem'],
    'Country': ['India', 'Czech Republic', 'Czech Republic', 'Germany', 'Pakistan'],
    'Attempt1': [87.03, 83.98, 79.79, 85.30, 82.40],
    'Attempt2': [87.58, np.nan, 80.30, 77.90, np.nan],
    'Attempt3': [76.79, np.nan, 85.44, 78.00, 84.62],
    'Attempt4': [np.nan, 82.86, np.nan, 83.10, 82.91],
    'Attempt5': [np.nan, 86.67, 84.98, 85.15, 81.98],
    'Attempt6': [84.24, np.nan, np.nan, 75.72, np.nan]
})
# display the dataframe
df

Output:

Dataframe showing the results of the men's javlin final the tokyo 2020 olympics

Here we created a dataframe containing the scores of the top five performers in the men’s javelin throw event final at the Tokyo 2020 Olympics. The attempts represent the throw of the javelin in meters.

To get the minimum value in a pandas column, use the min() function as follows. For example, let’s get the minimum distance the javelin was thrown in the first attempt.

# min value in Attempt1
print(df['Attempt1'].min())

Output:

79.79

We get 79.79 meters as the minimum distance thrown in the “Attemp1”

Note that you can get the index corresponding to the min value with the pandas idxmin() function. Let’s get the name of the athlete who threw the shortest in the first attempt with this index.

# index corresponding min value
i = df['Attempt1'].idxmin()
print(i)
# display the name corresponding this index
print(df['Name'][i])

Output:

2
Vitezslav Vesely

You can see that the min value corresponds to “Vitezslav Vesely”.

You can also get the min value of multiple pandas columns with the pandas min() function. For example, let’s find the minimum values in “Attempt1” and “Attempt2” respectively.

# get min values in columns "Attempt1" and "Attempt2"
print(df[['Attempt1', 'Attempt2']].min())

Output:

Attempt1    79.79
Attempt2    77.90
dtype: float64

Here, created a subset dataframe with the columns we wanted and then applied the min() function. We get the minimum value for each of the two columns.

Similarly, you can get the min value for each column in the dataframe. Apply the min() function over the entire dataframe instead of a single column or a selection of columns. For example –

# get min values in each column of the dataframe
print(df.min())

Output:

Name         Arshad Nadeem
Country     Czech Republic
Attempt1             79.79
Attempt2              77.9
Attempt3             76.79
Attempt4             82.86
Attempt5             81.98
Attempt6             75.72
dtype: object

We get the minimum values in each column of the dataframe df. Note that we also get min values for text columns based on their string comparisons in python.

If you only want the min values for all the numerical columns in the dataframe, pass numeric_only=True to the min() function.

# get min values of only numerical columns
print(df.min(numeric_only=True))

Output:

Attempt1    79.79
Attempt2    77.90
Attempt3    76.79
Attempt4    82.86
Attempt5    81.98
Attempt6    75.72
dtype: float64

What if you want to get the minimum value between two columns?
You can do so by using the pandas min() function twice. For example, let’s get the minimum value considering both “Attempt1” and “Attempt2”.

# min value over two columns
print(df[['Attempt1', 'Attempt2']].min().min())

Output:

77.9

We get 77.9 as the shortest distance considering the first and the second attempts together.

You can also get the single smallest value in the entire dataframe. For example, let’s get the smallest value in the dataframe df irrespective of the column.

# min value over the entire dataframe
print(df.min(numeric_only=True).min())

Output:

75.72

Here we apply the pandas min() function twice. First time to get the min values for each numeric column and then to get the min value among them.

For more on the pandas min() function, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Leave a Reply

Your email address will not be published. Required fields are marked *