Sling Academy
Home/Pandas/Pandas: How to get the Min/Max value of a Series

Pandas: How to get the Min/Max value of a Series

Last updated: February 18, 2024

Introduction

Pandas, a powerhouse tool in Python, offers extensive capabilities for data manipulation and analysis. At the core of its many features is the ability to easily determine the minimum and maximum values of a series, an operation that’s fundamental for data analysis tasks. This tutorial explores how to harness Pandas to get the minimum and maximum values from a series, with a gradual progression from basic to advanced examples. By the end, you’ll have a robust understanding of how to perform these operations efficiently.

Creating a Test Series

Before diving into the examples, let’s ensure you have the Pandas library installed. If not, you can install it using pip:

pip install pandas

Once installed, you can import Pandas and create a simple series to work with:

import pandas as pd
s = pd.Series([2, 3, 10, 6, 4, 8, 1])

This series will serve as our test data for demonstrating different methods to find the minimum and maximum values.

Basic Methods to Find Min/Max

To get started, the simplest way to find the minimum or maximum value of a series is by using the min() and max() methods:

print(s.min())
print(s.max())

Output:

1
10

These methods directly return the smallest and largest number in the series respectively.

Using idxmin() and idxmax() to Find Index Positions

An extension of the basic min and max calculations involves finding the index positions of these values using the idxmin() and idxmax() methods:

print(s.idxmin())
print(s.idxmax())

Output:

6
2

These methods are particularly useful when you want to know where the minimum or maximum values occur within your series.

Conditional Min/Max with Boolean Indexing

Often, you might be interested in finding minimum or maximum values under certain conditions. This is where boolean indexing becomes invaluable:

condition = s > 5
print(s[condition].min())
print(s[condition].max())

Output:

6
10

Here, condition filters the series to include only values greater than 5, upon which we then apply the min() and max() methods.

Using describe() for a Summary of Statistics

Pandas’ describe() method provides a snapshot of various summary statistics, including the min and max values:

print(s.describe())

Output:

count     7.0
mean      4.857143
std       3.120395
min       1.0
25%       3.0
50%       4.0
75%       8.0
max      10.0
dtype: float64

This method is especially helpful when you need a broader overview of your data alongside the min and max values.

Advanced: Calculating Min/Max with a Group

Moving to a more advanced usage, you can calculate the minimum and maximum values for groups within your data. For example, let’s consider a DataFrame:

df = pd.DataFrame({
    'group': ['A', 'B', 'A', 'B', 'A', 'B', 'A'],
    'value': [1, 23, 3, 45, 2, 67, 1]
})

You can group by the ‘group’ column and then apply the min() and max() functions to the ‘value’ column:

print(df.groupby('group')['value'].min())
print(df.groupby('group')['value'].max())

Output:

A     1
B    23
Name: value, dtype: int64

A     3
B    67
Name: value, dtype: int64

This method allows for nuanced analysis of how minimum and maximum values vary across different groups within your data.

Conclusion

Throughout this tutorial, we have explored multiple facets of how to get the minimum and maximum values from a Pandas series. From using straightforward methods like min() and max(), to employing more advanced techniques such as conditional filtering and grouping, Pandas provides a versatile set of tools for data analysis. Armed with these approaches, you can easily navigate through your datasets to find essential summary statistics that inform decision-making.

Next Article: Using pandas.Series.mean() to compute the arithmetic mean of a Series

Previous Article: Using Pandas Series.kurt() method to compute unbiased kurtosis

Series: Pandas Series: From Basic to Advanced

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)