Find Mean Absolute Deviation using mad() function in pandas
MAD the Mean Absolute Deviation is computed by summing up the absolute deviation of each data point from the mean of the dataset and the value is divided by the number of observations. pandas dataframe class has mad() which finds the MAD value for rows or columns for a dataframe instance.
Mean Absolute Deviation (MAD) is computed as the mean of absolute deviation of data points from their mean.
pandas DataFrame class has the method mad() that computes the Mean Absolute Deviation for rows or columns of a pandas DataFrame object.
When mad() is invoked with axis = 0, the Mean Absolute Deviation is calculated for the columns. When axis=1, MAD is calculated for the rows.
NaN values can be skipped using the optional Boolean parameter skipna. skipna=True skips the NaN values. skipna=False includes the NaN values. The default value for skipna is True.
Example 1
python
import pandas as pd
dataSet = {"C1":(6.5, 5.1, 5.6, 7.0, 7.1, 7.45, 7.75, 8),
"C2":(7, 7.1, 7.2, 6, 6.1, 6.3, 5.1, 5.2)
}
dataFrame = pd.DataFrame(data=dataSet);
print("DataFrame:");
print(dataFrame)
# Calculate Mean Absolute Deviation of DataFrame columns
mad = dataFrame.mad();
print("Mean absolute deviation of columns:");
print(mad);
# Calculate Mean Absolute Deviation of DataFrame rows
mad = dataFrame.mad(axis=1);
print("Mean absolute deviation of rows:");
print(mad);
Output
Example 2
python
import pandas as pd
dataValues = [(2, 3, 5, 7,),
(11, 13, 17, 19),
(23, 29, None, None),
(31, 37, None, None)]
dataFrameOject = pd.DataFrame(data=dataValues);
print("DataFrame:");
print(dataFrameOject)
# Calculate MAD for the DataFrame columns
mad = dataFrameOject.mad(axis=0);
print("Mean absolute deviation of columns(NaNs skipped):");
print(mad);
# Calculate MAD for the DataFrame columns
mad = dataFrameOject.mad(axis=0, skipna=False);
print("Mean absolute deviation of columns(NaNs included):");
print(mad);
# Calculate MAD for the DataFrame rows
mad = dataFrameOject.mad(axis=1);
print("Mean absolute deviation of rows(NaNs skipped):");
print(mad);
# Calculate MAD for the DataFrame rows
mad = dataFrameOject.mad(axis=1, skipna=False);
print("Mean absolute deviation of rows(NaNs included):");
print(mad);
Output
Your privacy choices
We'd like to use Google Analytics cookies to count visits and see which tutorials are useful.
They are only set if you accept. We don't use advertising cookies and we don't sell your data.
You can change your choice any time from “Your privacy choices” at the bottom of every page.
Privacy Policy
Your privacy choices
Your browser sent a Global Privacy Control signal, so analytics is off unless you turn it on here.
Strictly necessary
Remembers this choice in your browser. No cookies, nothing sent to anyone.Always on