To find the index of the maximum value of a column in pandas, the easiest way is to use the pandas idxmax() function.
df["Column"].idxmax()
If you are working with a Series object, you can also use idxmax() function.
series.idxmax()
Finding the index of the maximum value of numbers in a column in a DataFrame using pandas is easy. We can use the pandas idxmax() function to find the index of the maximum value in a column of numbers.
Let’s say we have the following DataFrame.
df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
'Weight': [160.20, 123.81, 209.45, 150.35, 102.43, 187.52]})
print(df)
# Output:
Name Weight
0 Jim 160.20
1 Sally 123.81
2 Bob 209.45
3 Sue 150.35
4 Jill 102.43
5 Larry 187.52
To get the maximum value using pandas in the column “Weight”, we can use the pandas max() function in the following Python code:
print(df["Weight"].max())
# Output:
209.45
From looking at the DataFrame above, we can see that the maximum value has index 2. We confirm that by using the idxmax function below:
print(df["Weight"].idxmax())
# Output:
2
If you are looking to find the index of the minimum value of a set of numbers, you can use the pandas idxmin() function.
Hopefully this article has been helpful for you to understand how to find the index of maximum value of numbers in a Series or DataFrame using idxmax() in pandas.