Python

Pandas Max(): Find The Max Value of a Pandas DataFrame Column

By ayed_amira , on 03/11/2021 , updated on 08/18/2021 - 3 minutes to read
pandas max

Pandas Max(): We will see in this tutorial how to use the max() function for a column in a Pandas dataframe.

Introduction

A pandas dataframe is a two-dimensional tabular data structure that can be modified in size with labeled axes that are commonly referred to as row and column labels, with different arithmetic operations aligned with the row and column labels.

The Pandas library, available on python, allows to import data and to make quick analysis on loaded data.

Max() is also included within Pandas Describe.

In this tutorial, we will see how to use the max() function of the Pandas library. This function allows to find the maximum value of a column DataFrame. We will see in this tutorial how to get :

  • The maximum value of a numeric column
  • The maximum value of a string column
  • The index of the maximum value of the column

To illustrate these different points, we will use this Pandas Dataframe:

import pandas as pd

powers = {'Heros': ['Thor', 'Spiderman', 'Iron man', 'Captain America', 'Hulk'],
          'Power': [50, 40, 55, 45, 80],
          'First_appearance': [1962, 1962, 1963, 1941, 1962]
        }
df = pd.DataFrame(powers, columns=['Heros', 'Power', 'First_appearance'])

print(df)

This dataframe contains 3 columns: the name of the hero, his power and the date of first appearance in the Marvel comics

Pandas Dataframe Max() function

Pandas Max() Syntax

The max() function provided in the Pandas library returns the maximum of the values for the requested axis.

Its syntax is as follows:

DataFrame.max(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

The function takes into consideration 5 parameters:

NameDescriptionTypeDefault ValueRequired
axisThe axis to apply the function ( 0=index,1=columns){index (0), columns (1)}Yes
skipnaExclude NA / NULL valuesBooleanTrueYes
levelIf the axis is a MultiIndex (hierarchical), count along a particular level, reducing to a series.int or level nameNoneYes
numeric_onlyInclude only float, int, boolean columns. If none, will try to use everything, then use only numeric data. Not implemented for the series.BooleanNoneYes
**kwargsAdditional arguments to be passed to the function.Yes

The function returns a Series or a Dataframe if the level is specified.

Pandas dataframe.max()

We will start from our Pandas dataframe :

import pandas as pd

powers = {'Heros': ['Thor', 'Spiderman', 'Iron man', 'Captain America', 'Hulk'],
          'Power': [50, 40, 55, 45, 80],
          'First_appearance': [1962, 1962, 1963, 1941, 1962]
        }
df = pd.DataFrame(powers, columns=['Heros', 'Power', 'First_appearance'])

print(df)
             Heros  Power  First_appearance
0             Thor     50              1962
1        Spiderman     40              1962
2         Iron man     55              1963
3  Captain America     45              1941
4             Hulk     80              1962

To calculate the maximum value of each column, we will use the max function with the parameter axis=0

# Max Value 

print(df['Heros'].max(axis=0))
print(df['Power'].max(axis=0))
print(df['First_appearance'].max(axis=0))
Thor
80
1963

The max() function works on both numeric and string columns.

Pandas dataframe.idxmax()

The max() function allows to get the maximum value of a dataframe column. There is a function called idxmax() which allows to get the index of the maximum value of the column.

# The index of the maximum value

print(df['Power'].idxmax())
print(df['First_appearance'].idxmax())
4
2

Conclusion

We have seen in this tutorial that it is quite simple to do arithmetic operations on columns of a pandas dataframe. The max() function is a very used function in data analysis to have a simple description of the data.

If you have any questions about its use, don’t hesitate to leave me a comment, I will be happy to answer them.

See you soon for new tutorials.

Back to Python Menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.