Pandas Tutorial
Creating Objects
Viewing Data
Selection
Manipulating Data
Grouping Data
Merging, Joining and Concatenating
Working with Date and Time
Working With Text Data
Working with CSV and Excel files
Operations
Visualization
Applications and Projects
The Mean Absolute Deviation (MAD) is a measure of the dispersion of a set of data points. It calculates the average of the absolute differences from the mean. In pandas, the mad()
function computes the MAD for a given Series or along a particular axis of a DataFrame.
Here's a tutorial on how to compute the Mean Absolute Deviation using pandas:
Firstly, ensure you have pandas installed:
pip install pandas
import pandas as pd
data = { 'A': [1, 2, 3, 4, 5], 'B': [5, 6, 7, 8, 10], 'C': [9, 8, 7, 6, 5] } df = pd.DataFrame(data) print(df)
You can compute the MAD for a particular column (Series) like this:
mad_A = df['A'].mad() print(f"Mean Absolute Deviation of Column 'A': {mad_A}")
To compute the MAD for all columns in a DataFrame:
mad_all = df.mad() print("Mean Absolute Deviation for each column:") print(mad_all)
Here's a simple breakdown of how MAD is calculated:
For a set of values:
x1,x2,…,xn
With a mean of:
xˉ
The MAD is:
MAD=n1∑i=1n∣xi−xˉ∣
So, it's the average of the absolute deviations of values from the mean.
The Mean Absolute Deviation offers a straightforward and interpretable measure of data dispersion. Pandas makes the computation of MAD very simple with its built-in .mad()
method. It's a useful metric when you want to understand the average variation in your dataset without squaring the deviations (as is done in variance and standard deviation).
Calculate mean absolute deviation in Pandas DataFrame:
mad_value = df.mad().mean()
Mean absolute deviation by axis in Pandas:
row_mad = df.mad(axis=1) column_mad = df.mad(axis=0)
Pandas mad() function examples:
.mad()
function directly computes mean absolute deviation.mad_value = df.mad()
Compute MAD for a specific column in Pandas:
column_mad = df['Column_Name'].mad()
Axis-wise mean absolute deviation in Pandas:
row_mad = df.mad(axis=1) column_mad = df.mad(axis=0)
Using mad() to calculate mean absolute deviation:
.mad()
function for MAD computation.mad_value = df.mad()
Pandas mad vs std for dispersion:
mad_value = df.mad() std_value = df.std()
Custom mean absolute deviation function in Pandas:
def custom_mad(data): return abs(data - data.mean()).mean() mad_value = df.apply(custom_mad)
Calculate robust mean absolute deviation in Pandas:
robust_mad = df.mad(axis=0, center='median')