DataFrame idxmin returning weird values

Question:

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?

Asked By: Omar

||

Answers:

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
Answered By: omar

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()
Answered By: BeRT2me
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.