get min and max values in python

How to Find Min & Max Values of Column in Pandas

Python pandas is a useful library for data processing and analytics. It allows you to organize data in a tabular format as columns and rows and use inbuilt functions to transform it. Often while working with pandas data sets, we need to find min and max values of column in Pandas. Here are the steps to do it in Python.


How to Find Min & Max Values of Column in Pandas

Let us say you have the following pandas dataframe with 4 rows and 4 columns.

df = pandas.DataFrame(randn(4,4))

You can use max() function to calculate maximum values of column. Here are 3 different ways to do this.

df.max(axis=0) # will return max value of each column
df.max(axis=0)['AAL'] # column AAL's max
df.max(axis=1) # will return max value of each row

In the first example above, we use axis=0 input to get max value of each column. In the next example, we specify column name AAL to get its maximum value. In the third example, we specify axis=1 to get each row’s max value.

You can also directly use the column name to get its maximum value.

df['AAL'].max()

Similarly, you can also use min() function to get minimum value of each function.

df.min(axis=0) # will return min value of each column
df.min(axis=0)['AAL'] # column AAL's min
df.min(axis=1) # will return min value of each row

In the first example above, we use min(axis=0) function to get minimum value of each column. In the next example, we use column name along with axis=0 argument to get minimum value of column. In the third example, we use axis=1 argument to get minimum value of each row.

If you know the column name, you can also directly reference it and call min() function to get the minimum value of that column.

df['AAL'].min()

In this article, we have learnt how to easily get max and minimum value of columns in python Pandas.

Also read:

How to Redirect Stdout & Stderr to File in Python
How to Extract Numbers from String in Python
How to Concatenate List Items to String in Python
How to Create Multiline String in Python
How to Put Variable in String in Python

Leave a Reply

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