search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas DataFrame | mad method

schedule Aug 11, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.mad(~) method computes the mean absolute deviation (MAD) for each row or column of the DataFrame.

Note that MAD is calculated like follows:

$$\mathrm{MAD}=\frac{1}{N}\sum_{i=0}^{N-1} |x_i-\bar{x}|$$

Where,

  • $N$ is the number of data points in the row/column

  • $x_i$ is the $i$-th value in the row/column

  • $\bar{x}$ is the mean of the row/column

Parameters

1. axislink | int or string | optional

Whether to compute the MAD row-wise or column-wise:

Axis

Description

"index" or 0

MAD is computed for each column.

"columns" or 1

MAD is computed for each row.

By default, axis=0.

2. skipnalink | boolean | optional

Whether or not to skip NaN. Skipped NaN would not count towards the total size ($N$), which is the divisor when computing MAD. By default, skipna=True.

3. level | string or int | optional

The name or the integer index of the level to consider. This is relevant only if your DataFrame is Multi-index.

Return Value

If the level parameter is specified, then a DataFrame will be returned. Otherwise, a Series will be returned.

Examples

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,4,6],"B":[2,5,8]})
df
A B
0 2 2
1 4 5
2 6 8

Computing MAD column-wise

To compute MAD for each column:

df.mad()
A 1.333333
B 2.000000
dtype: float64

Computing MAD row-wise

To compute MAD for each row:

df.mad(axis=1)
0 0.0
1 0.5
2 1.0
dtype: float64

Specifying skipna

Consider the following DataFrame:

df = pd.DataFrame({"A":[3,pd.np.nan,5]})
df
A
0 3.0
1 NaN
2 5.0

By default, skipna=True, which means that all missing values are ignored:

df.mad() # skipna=True
A 1.0
dtype: float64

To consider missing values:

df.mad(skipna=False)
A NaN
dtype: float64

With skipna=False, if a row/column contains one or more missing values, then the MAD for that row/column will be NaN.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!