## Finding Minimum Value across Multiple Columns in Pandas DataFrame

Data analysis is an essential aspect of any business today. With companies collecting an immense amount of data, finding trends and patterns can help make informed decisions.

Pandas is a widely used data manipulation library for Python programming language. It provides robust data structures for efficient data analysis, indexing, and merging.

In this article, we will look at methods for finding the minimum value across multiple columns in Pandas DataFrame. Method 1: Using min(axis=1)

One way to find the minimum value across multiple columns is by using the min(axis=1) method.

This method computes the minimum value across the rows, which is specified by axis=1. The resulting output will have a column with minimum values across rows.

Let us look at an example. Suppose we have a DataFrame containing the number of points and rebounds for three basketball players.

## import pandas as pd

data = {‘Player’:[‘Kobe’, ‘Lebron’, ‘Jordan’], ‘Points’:[25, 22, 30], ‘Rebounds’:[5, 10, 8]}

df = pd.DataFrame(data)

## print(df)

## Output:

Player Points Rebounds

## 0 Kobe 25 5

## 1 Lebron 22 10

## 2 Jordan 30 8

We can use min(axis=1) to find the minimum values for every row. df[‘Min’] = df.min(axis=1)

## print(df)

## Output:

Player Points Rebounds Min

## 0 Kobe 25 5 5

## 1 Lebron 22 10 10

## 2 Jordan 30 8 8

As you can see from the output, the new column ‘Min’ contains the minimum value for every row. This method is useful when you need to find the minimum value across all rows for a set of columns.

Method 2: Adding a New Column Containing Minimum Value

Another way to find minimum values across multiple columns is by adding a new column that contains the minimum value for each row. This method allows us to perform additional operations on the minimum values, such as filtering, sorting, or plotting.

To do this, we can use the apply method that applies a function to each row of the DataFrame. The function we will use is the Python built-in function min instead of Pandas min method.

After applying the min function to every row, the resulting output will be a new column added to the existing DataFrame. Let us look at an example.

Suppose we have a DataFrame containing the number of points and rebounds for three basketball players similar to the one used earlier. Firstly, let us define a function that returns the minimum value for every row.

def min_value(row):

return min(row[‘Points’], row[‘Rebounds’])

Secondly, we will use the apply method to apply min_value function to each row of the DataFrame. df[‘Min’] = df.apply(min_value, axis=1)

## print(df)

## Output:

Player Points Rebounds Min

## 0 Kobe 25 5 5

## 1 Lebron 22 10 10

## 2 Jordan 30 8 8

As you can see from the output, the new column ‘Min’ contains the minimum value for every row. This method offers more flexibility than the previous method as we can customize the minimum value function to meet our specific requirement.

Example 1: Finding Minimum Value Across Multiple Columns

Let us look at another example to see how these methods can be used in a practical scenario. Suppose, a company has a survey data for their employees containing the number of hours they spend working, sleeping, and exercising.

The company is interested in finding out the minimum number of hours that an employee spends working and exercising for the purpose of creating a new wellness program.

## import pandas as pd

data = {‘Employee’:[‘A’, ‘B’, ‘C’, ‘D’], ‘Working’:[6, 8, 7, 5], ‘Sleeping’:[7, 8, 9, 8], ‘Exercising’:[0, 1, 2, 3]}

df = pd.DataFrame(data)

## print(df)

## Output:

Employee Working Sleeping Exercising

## 0 A 6 7 0

## 1 B 8 8 1

## 2 C 7 9 2

## 3 D 5 8 3

We need to find the minimum number of hours an employee spends working and exercising. Method 1: Using min(axis=1)

df[‘Min’] = df[[‘Working’, ‘Exercising’]].min(axis=1)

print(df[[‘Employee’, ‘Working’, ‘Exercising’, ‘Min’]])

## Output:

Employee Working Exercising Min

## 0 A 6 0 0

## 1 B 8 1 1

## 2 C 7 2 2

## 3 D 5 3 3

Method 2: Adding a New Column Containing Minimum Value

def min_value(row):

return min(row[‘Working’], row[‘Exercising’])

df[‘Min’] = df.apply(min_value, axis=1)

print(df[[‘Employee’, ‘Working’, ‘Exercising’, ‘Min’]])

## Output:

Employee Working Exercising Min

## 0 A 6 0 0

## 1 B 8 1 1

## 2 C 7 2 2

## 3 D 5 3 3

In conclusion, finding minimum values across multiple columns in Pandas DataFrame is a useful operation in data analysis. There are several methods to approach this problem.

In this article, we explored two methods – Using min(axis=1) and Adding a New Column Containing Minimum Value. Depending on the specific use case, one method may be more suitable than the other.

By understanding these methods, you can make the most of Pandas and quickly analyze data for your projects. Example 2: Adding New Column Containing Minimum Value Across Multiple Columns

Let us explore how to use the Adding a New Column Containing Minimum Value method in another example.

Consider a dataset containing the sales data of a company for various years. The dataset has columns for year, product, sales, and expenses.

## import pandas as pd

data = {‘Year’:[2019, 2019, 2020, 2020], ‘Product’:[‘A’, ‘B’, ‘A’, ‘B’], ‘Sales’:[100, 200, 300, 400], ‘Expenses’:[50, 75, 100, 125]}

df = pd.DataFrame(data)

## print(df)

## Output:

Year Product Sales Expenses

## 0 2019 A 100 50

## 1 2019 B 200 75

## 2 2020 A 300 100

## 3 2020 B 400 125

Suppose, we want to add a new column containing the minimum value between sales and expenses for each year and product combination. We can start by defining a function that takes a row from the DataFrame and returns the minimum value between sales and expenses.

def min_value(row):

return min(row[‘Sales’], row[‘Expenses’])

Next, we can apply this function to each row of the DataFrame using the apply method. df[‘Min’] = df.apply(min_value, axis=1)

## print(df)

## Output:

Year Product Sales Expenses Min

## 0 2019 A 100 50 50

## 1 2019 B 200 75 75

## 2 2020 A 300 100 100

## 3 2020 B 400 125 125

As you can see from the output, a new column ‘Min’ has been added to the DataFrame containing the minimum value between sales and expenses for every year and product combination. This method is useful when you want to add new columns to the DataFrame based on some computation involving existing columns.

## Additional Resources

There are several other methods in Pandas DataFrame that can be used to find minimum values across multiple columns. Some of these methods include:

1.

Using the applymap method: This method applies a function element-wise to the DataFrame. We can use this method to find the minimum values across multiple columns by defining a function that returns the minimum value between two input values.

However, this method works only for smaller datasets as it can be slower for larger datasets. 2.

Using the numpy library: NumPy is a scientific computing library for Python. It provides efficient numerical operations on arrays.

We can use the numpy library’s minimum function to find the minimum values across multiple columns. This method is faster than using apply or applymap but requires additional knowledge of the numpy library.

In addition to the methods mentioned above, there are many other operations that can be performed in Pandas DataFrame, such as grouping, merging, and pivoting. To learn more about these operations and how to use them, there are several resources available online.

## Some useful resources include:

1. The Pandas documentation: The official Pandas documentation provides a comprehensive guide to using the Pandas library.

It includes detailed information on Pandas data structures, functions, and methods. 2.

Online courses: There are several online courses available that teach data analysis using Pandas. These courses can help you learn how to use Pandas effectively and efficiently for data analysis.

3. Stack Overflow: Stack Overflow is a community-driven platform where developers can ask and answer questions related to programming.

It has a dedicated section for Pandas, where you can find answers to common Pandas-related questions. By utilizing these resources, you can become proficient in using Pandas DataFrame for data analysis and make better decisions based on data insights.

In conclusion, this article explored different methods for finding the minimum value across multiple columns in Pandas DataFrame. The two primary methods discussed were Using min(axis=1) and Adding a New Column Containing Minimum Value.

Multiple examples were discussed to illustrate how these methods could be used in practical scenarios like calculating the minimum number of hours an employee works or adding new columns to the dataset containing minimum values. There are several other methods available, and online resources like the Pandas documentation, online courses, and Stack Overflow can facilitate the learning process for anyone interested in data analysis.

By understanding and applying these methods, one can make informed decisions using data insights to improve their work or business.