Home > other >  DataFrame idxmin returning weird values
DataFrame idxmin returning weird values

Time:07-18

I have a dataframe. I use

df[:10].idxmin()

Since I only want to take the index of the min value in the first 10 rows. But it gives me values for the 58th row and so on. What is wrong and what am I missing?

CodePudding user response:

iloc or head may give more reliable results here, your index may be messed up or something. You haven't provided enough information to be sure though.

df.iloc[:10].idxmin()
df.head(10).idxmin()

CodePudding user response:

Parameters for idxmin : axis : 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

use df.idxmin(axis = 0) for rows or df.idxmin(axis = 1) for column

example

df
   A   B   C
0  4  11   1
1  5   2   8
2  2   5  66
3  6   8   4

extract min col index for specific rows

>> df[:2]
    A   B  C
 0  4  11  1
 1  5   2  8

>> df[:2].idxmin(axis = 0) # indexed by row
output:
    A    0   #for first tow rows the min index in column 0 is found in row with index 0
    B    1   #for first tow rows the min index in column 1 is found in row with index 1
    C    0   #for first tow rows the min index in column 2 is found in row with index 0
>> df[:2].idxmin(axis = 1) #indexed by colum
output:
    0    C   #the min index in row 0 is found in column with index c
    1    B   #the min index in row 1 is found in column with index B

hope thats help

  • Related