Table of Contents
Finding the minimum value across multiple columns in Pandas refers to the process of identifying the lowest value present in a dataset that spans across multiple columns. This can be achieved by using built-in functions in Pandas such as “min()” or “idxmin()”, which allow for the efficient calculation and retrieval of the minimum value. By specifying the desired columns, the function will return the minimum value across those columns, making it a useful tool for data analysis and decision making.
Pandas: Find Minimum Value Across Multiple Columns
You can use the following methods to find the minimum value across multiple columns in a pandas DataFrame:
Method 1: Find Minimum Value Across Multiple Columns
df[['col1', 'col2', 'col3']].min(axis=1)
Method 2: Add New Column Containing Minimum Value Across Multiple Columns
df['new_col'] = df[['col1', 'col2', 'col3']].min(axis=1)
The following examples show how to use each of these methods in practice with the following pandas DataFrame:
import pandas as pd
#create DataFrame
df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
'points': [28, 17, 19, 14, 23, 26, 5],
'rebounds': [5, 6, 4, 7, 14, 12, 9],
'assists': [10, 13, 7, 8, 4, 5, 8]})
#view DataFrame
print(df)
player points rebounds assists
0 A 28 5 10
1 B 17 6 13
2 C 19 4 7
3 D 14 7 8
4 E 23 14 4
5 F 26 12 5
6 G 5 9 8Example 1: Find Minimum Value Across Multiple Columns
The following code shows how to find the minimum value in each row across the points and rebounds columns:
#find minimum value across points and rebounds columns
df[['points', 'rebounds']].min(axis=1)
0 5
1 6
2 4
3 7
4 14
5 12
6 5
dtype: int64
Here’s how to interpret the output:
- The minimum value across the points and rebounds columns for the first row was 5.
- The minimum value across the points and rebounds columns for the second row was 6.
- The minimum value across the points and rebounds columns for the third row was 4.
And so on.
Example 2: Add New Column Containing Minimum Value Across Multiple Columns
The following code shows how to add a new column to the DataFrame that contains the minimum value in each row across the points and rebounds columns:
#add new column that contains min value across points and rebounds columns
df['min_points_rebs'] = df[['points', 'rebounds']].min(axis=1)
#view updated DataFrame
print(df)
player points rebounds assists min_points_rebs
0 A 28 5 10 5
1 B 17 6 13 6
2 C 19 4 7 4
3 D 14 7 8 7
4 E 23 14 4 14
5 F 26 12 5 12
6 G 5 9 8 5The new column titled min_points_rebs now contains the minimum value across the points and rebounds columns for each row in the DataFrame.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I find the minimum value across multiple columns in Pandas?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-find-the-minimum-value-across-multiple-columns-in-pandas/
stats writer. "How can I find the minimum value across multiple columns in Pandas?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-find-the-minimum-value-across-multiple-columns-in-pandas/.
stats writer. "How can I find the minimum value across multiple columns in Pandas?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-find-the-minimum-value-across-multiple-columns-in-pandas/.
stats writer (2024) 'How can I find the minimum value across multiple columns in Pandas?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-find-the-minimum-value-across-multiple-columns-in-pandas/.
[1] stats writer, "How can I find the minimum value across multiple columns in Pandas?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I find the minimum value across multiple columns in Pandas?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
