Article From:https://www.cnblogs.com/chickenwrap/p/9971644.html

# 1. origin

pandas The idxmin and argmin inside look strange, so they want to make it clear according to the principle of one Py per day. Idxmax is similar to argmax, but today’s beauty chest is mini, just look at min. Max functionThe same principle.

# 2.1 Series.argmin Analysis

Start with Series. As you can see from the Series document, the argmin of Series is equal to the Series version of numpy’s ndarray. argmin, which is used to find the first minimum I.Ndex. Since Series’s index is returned, the data type should also match.

The test code is as follows

``````import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randn(10).reshape(2,5),columns=list('abcde'))
print(df)
``````
``````a         b         c         d         e
0  0.045384  0.484123  1.112115  0.037907  0.152338
1  0.151654 -0.056504  1.918992  1.057143  0.081632
``````

Test column Series and row Series, respectively.

``````a = df.a
print('a is')
print(a)
print('argmin of column a is %s.' % a.argmin())
print('*'*20)
A = df.loc[1]
print('A is ')
print(A)
print('argmin of row2 is %s.' % A.argmin())
``````
``````a is
0    0.045384
1    0.151654
Name: a, dtype: float64
argmin of column a is 0.
********************
A is
a    0.151654
b   -0.056504
c    1.918992
d    1.057143
e    0.081632
Name: 1, dtype: float64
argmin of row2 is b.
``````

Consistent with our expectations, we returned to the index.

## 2.2 Series.idxmin Analysis

Originally, I looked at idxmin in a similar way and found that it was exactly the same as argmin’s introduction on the document page.

This method is the Series version of ndarray.argmin.

I’m a bit confused. Is this not exactly the same function? Why are there two ways? After thinking about it, it seems that you can try to look at the source code. That’s the advantage of open source.

As a result, it’s much easier to think in the right way. The source code links of argmin and idxmin point to a link, the source code of idxmin. And it clearly says after the source code:

``````# ndarray compat
argmin = idxmin
argmax = idxmax
``````

Howl, alias only! uuuuuuuuuuuu

# 3. DataFrame.idxmin Analysis

DataFrame There is no argmin method, only idxmin. I’m wondering why Series has two identical functions. It’s supposed that the author wants to be compatible with numpy, and he himself prefers idxmin.

Since pandas are used, it’s better to keep pandas consistent. In the future, idxmin will be used. This function can specify different axes to return the minimum values for different Series, as follows

``````print(df.idxmin(axis=0))
print('*'*30)
print(df.idxmin(axis=1))
``````
``````a    0
b    1
c    0
d    0
e    1
dtype: int64
******************************
0    d
1    b
dtype: object
``````

# 4. summary

Today we discuss the argmin / idxmin function in pandas. For Series, these two are actually two different names of a function, as you can see from the source code; for DataFrame, they are not.Argmin, only idxmin.

So in order to maintain consistency, idxmin can be used uniformly in the future. This function returns the first index of the current object with a minimum value.

Author: Zhang Lidong