Coding Ref

In Pandas, the `idxmax`

method is used to return the index of the row with the maximum value in a specified column.

For example, consider the following DataFrame:

main.py

```
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
```

This DataFrame has three columns `A`

, `B`

, and `C`

, with five rows of data.

To find the index of the row with the maximum value in column `B`

, you could use the following code:

main.py

```
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
# Find the index of the row with the maximum value in column B
max_index = df['B'].idxmax()
# Print the maximum value and its index
print(f'The maximum value in column B is {df["B"].max()} at index {max_index}.')
```

output

```
The maximum value in column B is 50 at index 4.
```

In the code above, the `idxmax`

method is applied to the `B`

column of the DataFrame. This returns the index of the row with the maximum value in that column. In this case, the maximum value is 50, and it is located at index 4.

The `idxmax`

method can also be used with multiple columns.

For example, if you wanted to find the index of the row with the maximum value in either column `B`

or column `C`

, you could do the following:

main.py

```
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 3, 4, 5],
'B': [10, 20, 30, 40, 50],
'C': [100, 200, 300, 400, 500]
})
# Find the index of the row with the maximum value in columns B and C
max_index = df[['B', 'C']].idxmax()
# Print the maximum value and its index
print(f'The maximum value in columns B and C is {df[["B", "C"]].max().max()} at index {max_index}.')
```

output

```
The maximum value in columns B and C is 500 at index
B 4
C 4
dtype: int64.
```

In the code above, the `idxmax`

method is applied to the `B`

and `C`

columns of the DataFrame. This returns the index of the row with the maximum value in either of these two columns. In this case, the maximum value is 500, and it is located at index 4.

The `idxmax`

method will return the index of the first row with the maximum value, in the case where there are multiple rows with the same maximum value.

Related tutorials curated for you

How to make a crosstab in Pandas

How to drop duplicate columns in Pandas

How to calculate the variance in Pandas DataFrame

How to groupby, then sort within groups in Pandas

How to give multiple conditions in loc() in Pandas

How to use where() in Pandas

How to fix: AttributeError module 'pandas' has no attribute 'dataframe'

How to filter a Pandas DataFrame

How to round in Pandas

How to use qcut() in Pandas

How to find the minimum in Pandas

How to use pandas map() function