Pandas mad(), min(), max(), mean(), median(), and mode()

The Pandas DataFrame has several methods concerning Computations and Descriptive Stats. When applied to a DataFrame, these methods evaluate the elements and return the results.


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 mad()

The mad() method (Mean Absolute Deviation) is the average distance of all DataFrame elements from the mean.

To fully understand MAD from a mathematical point of view, feel free to watch this short tutorial:

The syntax for this method is as follows:

DataFrame.mad(axis=None, skipna=None, level=None)
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.

This example retrieves the MAD of four (4) Hockey Teams.

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

result = df_teams.mad(axis=0).apply(lambda x:round(x,3))
print(result)
  • Line [1] creates a DataFrame from a Dictionary of Lists and saves it to df_teams.
  • Line [2] uses the mad() method with the axis parameter set to columns to calculate MAD from the DataFrame. The lambda function formats the output to three (3) decimal places. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

Bruins2.000
Oilers2.444
Leafs3.111
Flames4.000
dtype:float64

DataFrame min()

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

The syntax for this method is as follows:

DataFrame.min(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 smallest 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.min(axis=0)
print(result)
  • Line [1] creates a DataFrame from a dictionary of lists and saves it to df_teams.
  • Line [2] uses the min() method with the axis parameter set to columns to retrieve the minimum value(s) from the DataFrame. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

Bruins4
Oilers3
Leafs2
Flames8
dtype:int64

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

Code Example 2

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

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

Output

[63 78 79 83]

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]

DataFrame mean()

The mean() method returns the average of the DataFrame/Series across a requested axis. If a DataFrame is used, the results will return a Series. If a Series is used, the result will return a single number (float).

The following methods can accomplish this task:

  • The DataFrame.mean() method, or
  •  The Series.mean() method

The syntax for this method is as follows:

DataFrame.mean(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 the average wins, losses, and ties for our Hockey Teams.

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.mean(axis=0).apply(lambda x:round(x,2))
print(result)
  • Line [1] creates a DataFrame from a Dictionary of Lists and saves it to df_teams.
  • Line [2] uses the mean() method with the axis parameter set to columns to calculate means (averages) from the DataFrame. The lambda function formats the output to two (2) decimal places. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

Bruins6.00
Oilers7.67
Leafs6.67
Flames12.00
dtype:float64

For this example, Alice Accord, an employee of Rivers Clothing, has logged her hours for the week. Let’s calculate the mean (average) hours worked per day.

Code Example 2

hours  = pd.Series([40.5, 37.5, 40, 55])
result = hours.mean()
print(result)
  • Line [1] creates a Series of hours worked for the week and saves hours.
  • Line [2] uses the mean() method to calculate the mean (average). This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

42.25

DataFrame median()

The median() method calculates and returns the median of DataFrame/Series elements across a requested axis. In other words, the median determines the middle number(s) of the dataset.

To fully understand median from a mathematical point of view, watch this short tutorial:

The syntax for this method is as follows:

DataFrame.median(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.

We will determine the median value(2) for our Hockey Teams for this example.

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

result = df_teams.median(axis=0)
print(result)
  • Line [1] creates a DataFrame from a dictionary of lists and saves it to df_teams.
  • Line [2] uses the median() method to calculate the median of the Teams. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

Bruins5.0
Oilers6.0
Leafs7.0
Flames8.0
dtype:float64

DataFrame mode()

The mode() method determines the most commonly used numbers in a DataFrame/Series.

The syntax for this method is as follows:

DataFrame.mode(axis=0, numeric_only=False, dropna=True)
ParameterDescription
axisIf zero (0) or index is selected, apply to each column. Default 0.
If one (1) apply to each row.
numeric_onlyOnly include columns that contain integers, floats, or boolean values.
dropnaIf set to True, this parameter ignores all NaN and NaT values. By default, this value is True.

For this example, we determine the numbers that appear more than once.

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

result = df_teams.mode(axis=0)
print(result)
  • Line [1] creates a DataFrame from a Dictionary of Lists and saves it to df_teams.
  • Line [2] uses the mode() method across the column axis. This output saves to the result variable.
  • Line [3] outputs the result to the terminal.

Output

 BruinsOilersLeafsFlames
04327
15949
2913713

You can see where the numbers come from in this visualization:


Further Learning Resources

This is Part 4 of the DataFrame method series.

  • Part 1 focuses on the DataFrame methods abs(), all(), any(), clip(), corr(), and corrwith().
  • Part 2 focuses on the DataFrame methods count(), cov(), cummax(), cummin(), cumprod(), cumsum().
  • Part 3 focuses on the DataFrame methods describe(), diff(), eval(), kurtosis().
  • Part 4 focuses on the DataFrame methods mad(), min(), max(), mean(), median(), and mode().
  • Part 5 focuses on the DataFrame methods pct_change(), quantile(), rank(), round(), prod(), and product().
  • Part 6 focuses on the DataFrame methods add_prefix(), add_suffix(), and align().
  • Part 7 focuses on the DataFrame methods at_time(), between_time(), drop(), drop_duplicates() and duplicated().
  • Part 8 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 9 focuses on the DataFrame methods equals(), filter(), first(), last(), head(), and tail()
  • Part 10 focuses on the DataFrame methods reset_index(), sample(), set_axis(), set_index(), take(), and truncate()
  • Part 11 focuses on the DataFrame methods backfill(), bfill(), fillna(), dropna(), and interpolate()
  • Part 12 focuses on the DataFrame methods isna(), isnull(), notna(), notnull(), pad() and replace()
  • Part 13 focuses on the DataFrame methods drop_level(), pivot(), pivot_table(), reorder_levels(), sort_values() and sort_index()
  • Part 14 focuses on the DataFrame methods nlargest(), nsmallest(), swap_level(), stack(), unstack() and swap_axes()
  • Part 15 focuses on the DataFrame methods melt(), explode(), squeeze(), to_xarray(), t() and transpose()
  • Part 16 focuses on the DataFrame methods append(), assign(), compare(), join(), merge() and update()
  • Part 17 focuses on the DataFrame methods asfreq(), asof(), shift(), slice_shift(), tshift(), first_valid_index(), and last_valid_index()
  • Part 18 focuses on the DataFrame methods resample(), to_period(), to_timestamp(), tz_localize(), and tz_convert()
  • Part 19 focuses on the visualization aspect of DataFrames and Series via plotting, such as plot(), and plot.area().
  • Part 20 focuses on continuing the visualization aspect of DataFrames and Series via plotting such as hexbin, hist, pie, and scatter plots.
  • Part 21 focuses on the serialization and conversion methods from_dict(), to_dict(), from_records(), to_records(), to_json(), and to_pickles().
  • Part 22 focuses on the serialization and conversion methods to_clipboard(), to_html(), to_sql(), to_csv(), and to_excel().
  • Part 23 focuses on the serialization and conversion methods to_markdown(), to_stata(), to_hdf(), to_latex(), to_xml().
  • Part 24 focuses on the serialization and conversion methods to_parquet(), to_feather(), to_string(), Styler.
  • Part 25 focuses on the serialization and conversion methods to_bgq() and to_coo().

Also, have a look at the Pandas DataFrame methods cheat sheet!