Functions
Math
MIN
How to Use Excel's MIN Function in Pandas
Excel's MIN function returns the smallest value from the numbers provided. This page shows how to implement Excel's MIN function in Python using pandas.
Use Mito's MIN function
Mito is an open source library that lets you write Excel formulas in Python. Either write the formula directly in Python or use the MIN formula in the Mito Spreadsheet and generate the equivalent Python code automatically.
Mito's MIN function works exactly like it does in Excel. That means you don't need worry about managing data types, handling errors, or the edge case differences between Excel and Python formulas.
Install Mito to start using Excel formulas in Python.
# Import the mitosheet Excel functions
from mitosheet.public.v3 import *;
# Use Mito's MIN function
df['min_value'] = MIN(df['A'], df['B'], 2)
Implementing the Minimum Value function in Pandas#
There are a few common ways to use the MIN function. Below are copy-ready Python implementations for some of the most common use cases.
Finding the Minimum Value between two numbers#
To find the minimum between two numbers in Excel, you might use a formula like =MIN(A1, B1).
In pandas, to achieve the same, you'd typically use Python's built-in `min` function:
min_value = min(1, 2)
Finding the Minimum Value Across Rows in dataframe#
To find the minimum values in each row of your pandas dataframe, you can use the `min` method along with specifying which columns to include in the row-wise calculation:
# Find the minimum value in column A, B, or C for each row
df['Min_Value'] = df[['A', 'B', 'C']].min(axis=1)
Finding the Minimum Value in a Series#
In Excel, if you want to find the minimum value in a column, you'd use the formula =MIN(A:A). The formula returns the smallest value in the range A:A.
Similarly in Pandas, you can use the `min` method on a Series to find the minimum value in a column:
# Find the minimum value in column A
min_value = df['A'].min()
Finding the Minimum in a dataframe#
If you want to determine the smallest value in an entire Excel sheet, you'd use the MIN function on the entire range. For example, if you had data in column A through G, you'd use the formula =MIN(A:G).
In pandas, to find the minimum value in an entire DataFrame, use the `min` function first to find the minimum value in each column, and again to find the minimum value among those results:
# Find the minimum value in the entire dataframe
min_value = df.min().min()
Common mistakes when using MIN in Python#
While using the MIN function in pandas, here are some common mistakes to watch out for, and how to resolve them.
Mismatched Data Types#
Attempting to find the minimum in a series with mixed data types can lead to a TypeError. It's essential to ensure data consistency before performing such operations.
# Convert to numeric and then find min
df['Column1'] = pd.to_numeric(df['Column1'], errors='coerce')
min_value = df['Column1'].min()
Ignoring NaN Values#
Just like Excel, by default, pandas excludes NaN values (aka missing values) when calculating the minimum. However, if the entire column or row contains only NaNs, the result will be NaN.
Understanding the Minimum Value Formula in Excel#
The MIN function in Excel takes one or more arguments (ranges of numbers or individual numbers) and returns the smallest value among them.
=MIN(number1, [number2], ...)
MIN Excel Syntax
Parameter | Description | Data Type |
---|---|---|
number1 | The first number you want to compare. | number |
number2 | (Optional) Additional numbers you want to compare. | number |
... | (Optional) Add up to 255 numbers you want to compare. | number |
Examples
Formula | Description | Result |
---|---|---|
=MIN(1, 5, 7) | Find the smallest value among 1, 5, and 7. | 1 |
=MIN(A1:A10) | Find the minimum value in the range A1 to A10. | Minimum in range |
Don't re-invent the wheel. Use Excel formulas in Python.
Install MitoDon't want to re-implement Excel's functionality in Python?
Edit a spreadsheet.
Generate Python.
Mito is the easiest way to write Excel formulas in Python. Every edit you make in the Mito spreadsheet is automatically converted to Python code.
View all 100+ transformations →