search
Search
Map of Data Science
search toc
Thanks for the thanks!
close
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to
Pandas
655 guides
keyboard_arrow_down
check_circle
Mark as learned
thumb_up
0
thumb_down
0
chat_bubble_outline
0
auto_stories new
settings

# Pandas DataFrame | mad method

schedule Mar 5, 2023
Last updated
local_offer
PythonPandas
Tags
expand_more
map
Check out the interactive map of data science

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 B0 2 21 4 52 6 8 

To compute MAD for each column:

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

To compute MAD for each row:

 df.mad(axis=1) 0 0.01 0.52 1.0dtype: float64 

## Specifying skipna

Consider the following DataFrame:

 df = pd.DataFrame({"A":[3,pd.np.nan,5]})df A0 3.01 NaN2 5.0 

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

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

To consider missing values:

 df.mad(skipna=False) A NaNdtype: float64 

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

Edited by 0 others
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!