Python Find Maximum & Minimum Index Position in pandas DataFrame (2 Examples)

In this Python tutorial you’ll learn how to identify the maximum and minimum value and the corresponding index in a pandas DataFrame.

Creation of Example Data

import pandas as pd                            # Load pandas
my_df = pd.DataFrame({'A':[5, 8, 1, 2, 7],    # Construct example DataFrame
                      'B':[1, 1, 1, 1, 1]})
print(my_df)                                  # Display example DataFrame in console
#    A  B
# 0  5  1
# 1  8  1
# 2  1  1
# 3  2  1
# 4  7  1

Example 1: Identify Max Value & the Corresponding Index in pandas DataFrame Column

print(my_df['A'].loc[my_df['A'].idxmax()])    # Get max
# 8
print(my_df['A'].idxmax())                    # Index of max
# 1

Example 2: Identify Min Value & the Corresponding Index in pandas DataFrame Column

print(my_df['A'].loc[my_df['A'].idxmin()])    # Get min
# 1
print(my_df['A'].idxmin())                    # Index of min
# 2

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top