Excel to Python: MIN Function - A Complete Guide | Mito
Home Icon
divider

Functions

divider

Math

divider

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.

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)
Copy!
Clipboard

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.

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)
Copy!
Clipboard

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)
Copy!
Clipboard

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()
Copy!
Clipboard

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()
Copy!
Clipboard

While using the MIN function in pandas, here are some common mistakes to watch out for, and how to resolve them.

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()
Copy!
Clipboard

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.

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

ParameterDescriptionData Type
number1The 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

FormulaDescriptionResult
=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 Mito

Don't want to re-implement Excel's functionality in Python?

Automate analysis with Mito