2.4 C
London
Friday, December 20, 2024
HomePandas in PythonDataFrame Functions in PythonPandas: How to Find Minimum Value Across Multiple Columns

Pandas: How to Find Minimum Value Across Multiple Columns

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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        8

Example 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                5

The new column titled min_points_rebs now contains the minimum value across the points and rebounds columns for each row in the DataFrame.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

Pandas: How to Move Column to Front of DataFrame
Pandas: How to Check if Column Contains String
Pandas: How to Add Empty Column to DataFrame

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories