0
0
PandasHow-ToBeginner · 3 min read

How to Use min and max in pandas for Data Analysis

In pandas, use min() and max() methods on DataFrames or Series to find the smallest and largest values respectively. These methods can be applied to entire data, columns, or rows with options to control axis and ignore missing values.
📐

Syntax

The min() and max() methods in pandas have this basic syntax:

  • DataFrame.min(axis=0, skipna=True)
  • DataFrame.max(axis=0, skipna=True)

Parameters explained:

  • axis=0: Find min/max for each column (down the rows).
  • axis=1: Find min/max for each row (across columns).
  • skipna=True: Ignore missing values (NaN) when calculating.
python
df.min(axis=0, skipna=True)
df.max(axis=0, skipna=True)
💻

Example

This example shows how to find the minimum and maximum values in a pandas DataFrame by column and by row.

python
import pandas as pd

data = {'A': [3, 5, 1, 7], 'B': [4, 2, 8, 6], 'C': [9, 3, 5, 2]}
df = pd.DataFrame(data)

# Minimum value in each column
min_by_column = df.min()

# Maximum value in each row
max_by_row = df.max(axis=1)

print('DataFrame:\n', df)
print('\nMinimum by column:\n', min_by_column)
print('\nMaximum by row:\n', max_by_row)
Output
DataFrame: A B C 0 3 4 9 1 5 2 3 2 1 8 5 3 7 6 2 Minimum by column: A 1 B 2 C 2 dtype: int64 Maximum by row: 0 9 1 5 2 8 3 7 dtype: int64
⚠️

Common Pitfalls

Common mistakes when using min() and max() in pandas include:

  • Not specifying axis correctly, which changes whether you get min/max by column or by row.
  • Ignoring missing values (NaN) which can cause unexpected results if skipna=False.
  • Using these methods on non-numeric columns without realizing they may return unexpected results or errors.
python
import pandas as pd

data = {'A': [3, None, 1], 'B': [4, 2, None]}
df = pd.DataFrame(data)

# Wrong: axis=1 when you want min by column
wrong_min = df.min(axis=1)

# Right: axis=0 for min by column
right_min = df.min(axis=0)

print('Wrong min (axis=1):\n', wrong_min)
print('\nRight min (axis=0):\n', right_min)
Output
Wrong min (axis=1): 0 3.0 1 2.0 2 1.0 dtype: float64 Right min (axis=0): A 1.0 B 2.0 dtype: float64
📊

Quick Reference

FunctionDescriptionDefault axisSkip NaN
min()Find minimum value0 (columns)True
max()Find maximum value0 (columns)True

Key Takeaways

Use df.min() and df.max() to find minimum and maximum values in pandas DataFrames or Series.
Set axis=0 to compute by column and axis=1 to compute by row.
skipna=True ignores missing values (NaN) by default for accurate results.
Be careful with axis parameter to avoid unexpected outputs.
These methods work best on numeric data columns.