What is the dataframe.mad() function in pandas?

Overview

The mad() function in pandas lets us obtain the mean absolute deviation of values contained in the specified axis of the given data frame.

Mathematically, the mean absolute deviation of a given data is calculated as follows, where:

m(x)m(x) = mean value of the data

nn = number of data values

xix_i= the data values

1ni=1nxim(X)\LARGE{\frac{1}{n} \sum_{i=1} ^{n} \mid x_i - m(X)\mid}

Syntax

The syntax of the mad() function is shown below:

DataFrame.mad(axis=None, skipna=True, level=None)
Syntax for the mad() function in Pandas

Parameter value

The mad() function takes the following parameter values:

  • axis: This represents the name of the row (designated as 0 or 'index') or the column (designated as 1 or columns) axis.
  • skipna: This takes a boolean value that indicates whether N/A or null values are to be excluded.
  • level: This takes an int value that specifies the count with a particular level.

Return value

The mad() function returns a DataFrame that holds the result.

Example

# A code to illustrate the mad() 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 mean absolute deviation vertically across the rows
print(df.mad())
# Obtaining the cumulative maximum horizontally over the columns
print(df.mad(axis="columns"))

Explanation

  • Line 4: We import the Pandas library.
  • Lines 7–10: We create a DataFrame, df.
  • Line 12: We print df.
  • Line 15: Using the mad() function, we obtain the mean absolute deviation of the values running downwards across the rows (axis 0). We print the result to the console.
  • Line 18: Using the mad() function, we obtain the mean absolute deviation of the values running horizontally across the columns (axis 1). We print the result to the console.

Free Resources