Get Max & Min Value of Column & Index in pandas DataFrame in Python (2 Examples)

 

In this Python article you’ll learn how to find the maximum and minimum value and the corresponding row index position in a pandas DataFrame.

The post contains this content:

So now the part you have been waiting for – the examples…

 

Example Data & Add-On Libraries

In order to use the functions of the pandas library, we first need to load pandas:

import pandas as pd                                # Load pandas library

Furthermore, consider the following example data:

data = pd.DataFrame({'x1':[3, 2, 7, 1, 9, 3, 4],  # Create example DataFrame
                     'x2':[2, 1, 1, 3, 1, 2, 3],
                     'x3':range(7, 0, - 1)})
print(data)                                       # Print example DataFrame

 

table 1 DataFrame get max min value index pandas dataframe python

 

Have a look at the previous table. It shows that our example data consists of seven rows and three columns.

 

Example 1: Find Max & Min Value in pandas DataFrame Column

In Example 1, I’ll explain how to return the maximum and minimum value contained in a particular pandas DataFrame variable.

To find the maximum value of the column x1, we can use the loc attribute and the idxmax function as shown below:

my_max = data['x1'].loc[data['x1'].idxmax()]      # Maximum in column
print(my_max)
# 9

The previous Python console output shows the max value in the column x1, i.e. the value 9.

Similar to that, we can use the idxmin function to search and find the minimum value in a column:

my_min = data['x1'].loc[data['x1'].idxmin()]      # Minimum in column
print(my_min)
# 1

The smallest number in the column x1 is the number 1.

 

Example 2: Find Index of Max & Min Value in pandas DataFrame Column

The previous example has explained how to get the maxima and minima of a pandas DataFrame column.

This example shows how to find the row index positions that correspond to the max and min values.

The following Python code returns the index position of the maximum value in the variable x1:

my_max_ind = data['x1'].idxmax()                  # Index of maximum in column
print(my_max_ind)
# 4

The max value in the column x1 can be found in the row with the index number 4.

Similar to that, we can execute the following Python code to identify the row index of the minimum value:

my_min_ind = data['x1'].idxmin()                  # Index of minimum in column
print(my_min_ind)
# 3

The index position of the minimum value is 3.

 

Video, Further Resources & Summary

Have a look at the following video on the YouTube channel of Paul Miskew to get more information on maxima and minima in Python. In the video instruction, the speaker explains how to find the max and min values in a list object:

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may want to read some of the related Python tutorials on this homepage. A selection of tutorials is listed below:

 

To summarize: On this page, I have explained how to identify the maximum and minimum value and the corresponding index in a pandas DataFrame in the Python programming language. In case you have further questions, don’t hesitate to let me know in the comments.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top