How to obtain the maximum value of an axis of a pandas DataFrame

Overview

The max() function is used to return the maximum values of a specified axis.

Note: Axis here represents the rows and columns of the DataFrame. An axis with a value of '0' indicate the axes running vertically downwards across a row, while a value of '1' indicates the axes running running horizontally across a column.

Syntax

DataFrame.max(axis=None, skipna=True, *args, **kwargs)
Syntax for the cummax() function in Pandas

Parameters

  • axis: This represents the name for the row (designated as 0 or 'index') or the column (designated as 1 or columns) axis.
  • skipna: This takes a Boolean value indicating if null values are to be excluded. This is an optional parameter.
  • level: This takes an integer or a level name specifying the count along a particular level.
  • numeric_only: This takes a boolean value indicating whether to include only float, integer, or boolean columns.
  • **kwargs: This is a keyword that has no effect but may be accepted for compatibility with NumPy. This is an optional parameter.

Return value

This function returns a Series or DataFrame object showing the maximum values in the specified axis.

Example

# A code to illustrate the max() function in Pandas
# importing the pandas library
import pandas as pd
# creating a dataframe
df = pd.DataFrame([[5,10,4,15,3],
[1,7,5,9,0.5],
[3,11,13,14,12]],
columns=list('ABCDE'))
# printing the dataframe
print(df)
# obtaining the maximum values vertically across rows
print(df.max(axis="index"))
# obtaining the maximum values horizontally over columns
print(df.max(axis="columns"))

Explanation

  • Line 4 : We import the pandas library.
  • Lines 7–10: We create a DataFrame, df.
  • Line 12: We print the DataFrame, df.
  • Line 15: We use the max() function to obtain the maximum values running downwards across the rows (axis 0). We print the result to the console.
  • Line 18: We use the max() function to obtain the maximum values running horizontally across columns (axis 1). We print the result to the console.

Free Resources