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.
variablename_df = pandas.read_csv('yourspreadsheet.csv')
print (variablename_df.Year.min(), variablename_df.Year.max())
Breaking down the code
- Import the Pandas Library
- Pull your CSV file into Python using the “read_csv” function
- 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.