Pandas: Minimum and Maximum Values

Today we will look at retrieving the minimum and maximum values in a pandas column. Lets say we have various dates in a dates column and we want to see what the earliest date in the set was or what the latest date may have been. This is a very simple and easy to use function built into the pandas library so we do not have to create it by hand using a loop.

In this example we are looking to find out the “Year” column range in a set of data.

Python code:

import pandas
variablename_df = pandas.read_csv('yourspreadsheet.csv')
print (variablename_df.Year.min(), variablename_df.Year.max())

It will return: (1987,2008)

Breaking down the code

  1. Import the Pandas Library
  2. Pull your CSV file into Python using the “read_csv” function
  3. Here we use both the “min” and “max” function in the same line but they can be separated for use.
    • “variablename_df” is the DataFrame we created from the CSV file
    • “.Year” is the name of the Column in the DataFrame that we want to use.
    • “.min” or “.max” is letting pandas know if you want to pull the minimum or maximum value.

Leave a Reply

Your email address will not be published. Required fields are marked *