Pandas DataFrame | min method
Start your free 7-days trial now!
Pandas DataFrame.min(~) method computes the minimum of each row or column of the DataFrame.
Parameters
1. axislink | int or string | optional
Whether to compute the minimum row-wise or column-wise:
Axis | Description |
|---|---|
| Minimum is computed for each column. |
| Minimum is computed for each row. |
By default, axis=0.
2. skipnalink | boolean | optional
Whether or not to skip NaN. By default, skipna=True.
3. level | string or int | optional
The name or the integer index of the level to consider. This is needed relevant if your DataFrame is Multi-index.
4. numeric_onlylink | None or boolean | optional
The allowed values are as follows:
Value | Description |
|---|---|
| Only numeric rows/columns will be considered (e.g. |
| Attempt computation with all types (e.g. strings and dates), and throw an error whenever the minimum cannot be computed. |
| Attempt computation with all types, and ignore all rows/columns whose minimum cannot be computed without raising an error. |
Note that a minimum can only be computed when the < operator is well-defined between the types.
By default, numeric_only=None.
Return Value
If the level parameter is specified, then a DataFrame will be returned. Otherwise, a Series will be returned.
Examples
Consider the following DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})df
A B0 2 41 3 5
Column-wise minimum
To compute the minimum for each column:
df.min() # or axis=0
A 2B 4dtype: int64
Row-wise minimum
To compute the minimum for each row, set axis=1:
df.min(axis=1)
0 21 3dtype: int64
Specifying skipna
Consider the following DataFrame with a missing value:
df = pd.DataFrame({"A":[4,pd.np.nan]})df
A0 4.01 NaN
By default, skipna=True, which means that missing values will be ignored:
df.min() # skipna=True
A 4.0dtype: float64
To consider missing values:
df.min(skipna=False)
A NaNdtype: float64
Note that the minimum of a row/column that contains a missing value will be NaN.
Specifying numeric_only
Consider the following DataFrame:
df = pd.DataFrame({"A":[4,5], "B":[2,True], "C":["6",False]})df
A B C0 4 2 "6"1 5 True False
Here, both columns B and C contain mixed types, but the key difference is that the minimum is defined for B, but not for C. Computing the minimum requires comparison operators (< and >) between the types to be well-defined.
Recall that the internal representation of a True boolean is 1, so the operation 2>True is defined:
2 > True
True
On the other hand, "6">False throws an error:
"6" > False
TypeError: '>' not supported between instances of 'str' and 'bool'
None
By default, numeric_only=None, which means that rows/columns with mixed types will also be considered:
df.min(numeric_only=None)
A 4B Truedtype: object
Here, notice how the minimum was computed for column B, but not for C. By passing in None, rows/columns where the minimum cannot be computed (due to undefined < and > between types) will simply be ignored without raising an error.
False
By setting numeric_only=False, rows/columns with mixed types will again be considered, but an error will be thrown when the minimum cannot be computed:
df.min(numeric_only=False)
TypeError: '<=' not supported between instances of 'str' and 'bool'
Here, we end up with an error because column C contains mixed types where < is not defined.
True
By setting numeric_only=True, only numeric rows/columns will be considered:
df.min(numeric_only=True)
A 4dtype: int64
Notice how columns B and C were ignored this time since they contain non-numeric types.