Minimum of DataFrame over an Axis
To find the minimum of the values over the requested axis in a DataFrame in Pandas, call min() method on this DataFrame. DataFrame.min() method returns a Series with values containing the minimum values over specified axis.
In this tutorial, we will learn the syntax of DataFrame.min() method, and how to use this method to find the minimum of values over a specified axis of DataFrame.
Syntax
The syntax of pandas DataFrame.min() method is
DataFrame.min(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
where
Parameter | Value | Description |
---|---|---|
axis | {index (0), columns (1)}. default value is 0. | Axis for the function to be applied on. |
skipna | bool. default value is True. | Exclude NA/null values when computing the result. |
level | int or level name. default value is None. | If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series. |
numeric_only | bool. default value is None. | Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series. |
**kwargs | Additional keyword arguments to be passed to the function. |
Return Value
- Series or
- DataFrame (if level specified)
Examples
Minimum in Columns of DataFrame
By default, minimum is calculated for columns in a DataFrame.
In the following program, we take a DataFrame two columns containing numerical data, and find the minimum in columns, or we can say over index axis of this DataFrame.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, 4], 'b': [3, 2]})
result = df.min()
print(result)
Output
a 4
b 2
dtype: int64
Minimum of Rows of DataFrame
To compute the minimum values in rows of DataFrame, pass axis=1
in call to DataFrame.min() method.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, 4], 'b': [3, 2]})
result = df.min(axis=1)
print(result)
Output
0 3
1 2
dtype: int64
Do not skip NA while finding Minimum
By default, NA values like None, np.nan, etc are ignored. But if we would like consider those values as well, pass skipna=False
to DataFrame.min() method.
Example.py
import pandas as pd
df = pd.DataFrame({'a': [7, None], 'b': [3, 2]})
result = df.min(skipna=False)
print(result)
Output
a NaN
b 2.0
dtype: float64
If any of the values is NA along the specified axis, then NaN
would be considered as minimum value.
Conclusion
In this Pandas Tutorial, we learned how to find the minimum of values over specified axis in a DataFrame using pandas DataFrame.min() method.