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:
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 as0or'index') or the column (designated as1orcolumns) axis.skipna: This takes a boolean value that indicates whetherN/Aornullvalues are to be excluded.level: This takes anintvalue 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 libraryimport pandas as pd# Creating a DataFramedf = pd.DataFrame([[5,10,4,15,3],[1,7,5,9,0.5],[3,11,13,14,12]],columns=list('ABCDE'))# Printing the DataFrameprint(df)# Obtaining the mean absolute deviation vertically across the rowsprint(df.mad())# Obtaining the cumulative maximum horizontally over the columnsprint(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 (axis0). 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 (axis1). We print the result to the console.