Pandas DataFrame idxmin() method with examples

Spread the love

The pandas idxmin() method returns the column/row label of the minimum value of each row/column of the source DataFrame. NA/null values are excluded.

Syntax –

DataFrame.idxmin(axis=0, skipna=True)

axis – The axis to use. 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise.

skipna – Exclude NA/null values. If an entire row/column is NA, the result will be NA.

Examples –

Let’s create a dataframe to work with.

import pandas as pd

data = {'Apple':[89, 89, 90, 110, 125, 84, 131, 123, 123, 140, 145, 145],
       'Orange': [46, 46, 50, 65, 63, 48, 110, 120, 60, 42, 47, 62],
       'Banana': [26, 30, 30, 25, 38, 22, 22, 36, 20, 27, 23, 34 ],
       'Mango': [80, 80, 90, 125, 130, 150, 140, 140, 135, 135, 80, 90]}

index = ['Jan','Feb','Mar','Apr','May','June','Jul','Aug','Sep','Oct','Nov','Dec']
df = pd.DataFrame(data, index=index)
df

1 . Find the Index that has Minimum value for Each Columns –

To find the index that has minimum value for each columns, we can use the idxmin() method in pandas.

df.idxmin()
#output
Apple     June
Orange     Oct
Banana     Sep
Mango      Jan
dtype: object

The cost of Apples is lowest in June. For Orange it’s Oct, for Banana it is Sep and for Mango is it Jan.

2 .Find Column that has Minimum value For Each Row –

To find the column that has minimum value for each row, we need to set the axis parameter to axis=1 or columns.

df.idxmin(axis=1)
#output
Jan     Banana
Feb     Banana
Mar     Banana
Apr     Banana
May     Banana
June    Banana
Jul     Banana
Aug     Banana
Sep     Banana
Oct     Banana
Nov     Banana
Dec     Banana
dtype: object

Throughout the year, Banana costs less than any other fruits in this dataframe.

Related Posts –

  1. Pandas DataFrame idxmax() method with examples

Rating: 1 out of 5.

Leave a Reply