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 |
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 |
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'].loc[my_df['A'].idxmax()]) # Get max # 8
print(my_df['A'].idxmax()) # Index of max # 1 |
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'].loc[my_df['A'].idxmin()]) # Get min # 1
print(my_df['A'].idxmin()) # Index of min # 2 |
print(my_df['A'].idxmin()) # Index of min # 2