How to obtain the minimum value of a desired axis of a DataFrame
Pandas DataFrame is a two-dimensional data structure hosting data aligned in a tabular fashion. It has the following properties:
Columns can have different types
Mutable size
Ability to perform arithmetic operations on rows and columns
Axes (rows and columns) are labeled
With a pandas DataFrame, we can leverage the min() method which returns a series of values representing the minimum values over a specified axis.
The syntax of this method is as follows:
DataFrame.min(axis=0, skipna=True, numeric_only=False, **kwargs)
The min() function accepts the following parameters:
axis: Indicates where the function will be applied (0 represents columns and 1 represents rows).
skipna: Computes results without including null values.
numeric_only: Includes only float, int, and boolean columns. Not implemented for Series.
**kwargs: Additional keyword arguments to be passed to the function.
Let's try some examples to find the minimum values over a requested axis of a DataFrame.
Code example 1
The following example shows how to extract the lowest values per column along the index axis in the given DataFrame:
import pandas as pddf = pd.DataFrame({'Name': ['Bassem','Celeste','Rachel','Angel'], 'Age': [45, 33 , 32 , 258], 'Salary': [2500, 3000, 2000, 1000], 'Grade': [76, 88 , 25 , 66]})print("The DataFrame is:")print(df.head(5))print("The Lowest values per column:")print(df.min(axis=0))print("The Lowest values per column while considering numerical values only:")print(df.min(numeric_only = True,axis=0))
Let's go through the above code:
Line 1: We import the pandas library.
Lines 3–8: We define a sample DataFrame containing employees' information.
Lines 10–11: We print out the DataFrame.
Line 13: We invoke the
df.min()function while specifyingaxis=0as a parameter, indicating that the column's values should be considered.Line 14: We display the extracted result.
Lines 16–17: We invoke the
df.min()function again while specifying an additional argumentnumeric_only = Truein order to consider columns containing numerical values only.
Code example 2
The following code example shows how to retrieve the minimum values per row along the column axis, in the given DataFrame:
import pandas as pddf = pd.DataFrame({'Name': ['Bassem','Celeste','Rachel','Angel'], 'Age': [45, None, 32, 258], 'Salary': [2500, 3000, 2000, 1000], 'Grade': [76, 88, 25, None]})print("The DataFrame is:")print(df.head(5))print("The Lowest values per row:")print(df.min(axis=1))
Let's go through the above code widget:
Line 1: We import the pandas library.
Lines 3–8: We define a sample DataFrame containing employees' information.
Lines 10–11: We print out the Data Frame.
Lines 13: We invoke the
df.min()function while specifyingaxis=1as the parameter indicating that the row's values should be consideredLine 14: We display the extracted result. Notice that NaN values are ignored by default but if we want to include the NaN values, we can add the following argument as a parameter to the
df.min()function:skipna=False
Free Resources