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:
The syntax of the mad() function is shown below:
DataFrame.mad(axis=None, skipna=True, level=None)
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. The mad() function returns a DataFrame that holds the result.
# 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"))
df.df.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.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.