import pandas as pd
df = pd.DataFrame({
'Latency_ms': [120, 45, 88],
'Cost_USD': [15, 25, 10]
}, index=['Server_A', 'Server_B', 'Server_C'])
print(df.idxmin())
Expected Output:
Latency_ms Server_B
Cost_USD Server_C
dtype: object
The DataFrame.idxmin() method returns the index label associated with the minimum numerical value along a designated DataFrame axis. In performance optimization, cost reduction models, and error telemetry, data engineers frequently seek to pinpoint lowest cost vendors, minimum latency nodes, or lowest risk metric indexes. idxmin() returns index labels directly rather than just scalar numerical minimums.
DataFrame.idxmin(axis=0, skipna=True, numeric_only=False)axis: 0 or 'index' (row index label of column minimum; default), 1 or 'columns' (column label of row minimum).skipna: Boolean (default True). Excludes missing values during calculation.numeric_only: Boolean (default False). Restricts calculations to numeric datatypes.Using axis=1 identifies the column name containing the minimum value for each row record.
import pandas as pd
df_shipping = pd.DataFrame({
'DHL': [45.0, 60.0],
'FedEx': [42.0, 65.0],
'UPS': [48.0, 58.0]
}, index=['Order_1', 'Order_2'])
df_shipping['Best_Carrier'] = df_shipping.idxmin(axis=1)
print(df_shipping)
Expected Output:
DHL FedEx UPS Best_Carrier
Order_1 45.0 42.0 48.0 FedEx
Order_2 60.0 65.0 58.0 UPS
idxmin() returns index tuples.Index.get_level_values() to extract specific hierarchical index levels.
Q: How does idxmin() differ from argmin()?
argmin() (available on NumPy arrays or Series) returns the 0-based integer positional index (e.g., 0, 1, 2). idxmin() returns the label of the index (e.g., 'Server_B' or '2026-05-10').
Explore interactive code cells, test carrier cost minimization, and execute self-validating assertions.
View on GitHubAuthor & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.