Pandas DataFrame max() Method


Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import numpy as np 

DataFrame max()

The max() method returns the largest value(s) from a DataFrame/Series. The following methods can accomplish this task:

The syntax for this method is as follows:

DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
ParameterDescription
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
skipnaIf this parameter is True, any NaN/NULL value(s) ignored. If False, all value(s) included: valid or empty. If no value, then None is assumed.
levelSet the appropriate parameter if the DataFrame/Series is multi-level. If no value, then None is assumed.
numeric_onlyOnly include columns that contain integers, floats, or boolean values.
**kwargsThis is where you can add additional keywords.

For this example, we will determine which Team(s) have the most significant amounts of wins, losses, or ties.

Code Example 1

df_teams = pd.DataFrame({'Bruins':    [4, 5,  9],
                         'Oilers':    [3, 6, 14],
                         'Leafs':     [2, 7, 11],
                         'Flames':    [21, 8, 7]})

result = df_teams.max(axis=0)
print(result)
  • Line [1] creates a DataFrame from a Dictionary of Lists and saves it to df_teams.
  • Line [2] uses max() with the axis parameter set to columns to retrieve the maximum value(s) from the DataFrame. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

Bruins9
Oilers14
Leafs11
Flames21
dtype:int64

This example uses two (2) arrays and retrieves the Series’s maximum value(s).

Code Example 2

c11_grades = [63, 78, 83, 93]
c12_grades = [73, 84, 79, 83]

result = np.maximum(c11_grades, c12_grades)
print(result)
  • Line [1-2] creates lists of random grades and assigns them to the appropriate variable.
  • Line [3] uses the NumPy library maximum function to compare the two (2) arrays. This output saves to the result variable.
  • Line [4] outputs the result to the terminal.

Output

[73 84 83 93]

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.