Home > Back-end >  How to find the row having the minimum values in a given pandas dataframe?
How to find the row having the minimum values in a given pandas dataframe?

Time:06-15

I have created a dataframe using the following code.

df = pd.read_csv('state_cpi.csv')
data = df.iloc[:,3:]

Then I found the minimum values of each row by using

data.min()

It gave me the minimum value of each column, but I also want to find out that at which index the min value is available for each column. Please give a sutiable solution for finding the index of the min value of each column. Dataset in this repo

CodePudding user response:

First, you have to fix your data. You have -- as value for the cell (347, Himachal Pradesh) then you can use idxmin as suggested:

df = pd.read_csv('state_cpi.csv', na_values='--')
out = df.iloc[:, 3:].idxmin()

Output:

>>> out
Andhra Pradesh            388
Arunachal Pradesh         388
Assam                     388
Bihar                     388
Chattisgarh               388
Delhi                     388
Goa                       388
Gujarat                   388
Haryana                   388
Himachal Pradesh          388
Jharkhand                 388
Karnataka                 388
Kerala                    388
Madhya Pradesh            388
Maharashtra               388
Manipur                   388
Meghalaya                 388
Mizoram                   358
Nagaland                  388
Orissa                    388
Punjab                    388
Rajasthan                 388
Sikkim                    388
Tamil Nadu                388
Telangana                 388
Tripura                   388
Uttar Pradesh             388
Uttarakhand               388
West Bengal               388
Andaman and Nicobar       388
Chandigarh                388
Dadra and Nagar Haveli    388
Daman and Diu             388
Jammu and Kashmir         388
Lakshadweep               388
Puducherry                388
dtype: int64

Note: it's really curious but the min for each column is the row 388. Try to use idxmax to see the difference.

CodePudding user response:

Use .idxmin()

Example:

my_min_indexes = data.idxmin(axis=0)

CodePudding user response:

You can try:

data.idxmin()

That should do it.

  • Related