+

Python | Pandas dataframe.mad ()

The Pandas function dataframe.mad() returns the mean absolute deviation of the values ​​for the requested axis. Average absolute deviation of the dataset — this is the average distance between each data point and the mean. This gives us an idea of ​​the variability in the dataset.

Syntax: DataFrame.mad (axis = None, skipna = None, level = None)

Parameters:
axis: {index (0), columns (1)}
skipna: Exclude NA / null values ​​when computing the result
level: If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
numeric_only: Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.

Returns: mad: Series or DataFrame (if level specified)

Example # 1: Use the mad () function to find the mean absolute deviation of the values ​​along the index axis.

# import pandas as pd

import pandas as pd

 
# Create data frame

df = pd.DataFrame ({ "A" : [ 12 , 4 , 5 , 44 , 1 ],

  "B" : [ 5 , 2 , 54 , 3 , 2 ], 

"C" : [ 20 , 16 , 7 , 3 , 8 ],

"D" : [ 14 , 3 , 17 , 2 , 6 ]})

 
# Print data frame
df

Let`s use the dataframe.mad () function to find the average absolute deviation.

 

# find the average absolute deviation
# along the index axis

df.mad (axis = 0 )

Output:

Example # 2: Use the mad () function to find the mean absolute deviation of values ​​along the axis of a column that has some Na values.

# import pandas as pd

import pandas as pd

  
# Create data frame

df = pd.DataFrame ({ " A " : [ 12 , 4 , 5 , None , 1 ],

"B" : [ 7 , 2 , 54 , 3 , None ],

"C" : [ 20 , 16 , 11 , 3 , 8 ], 

"D" < / code> : [ 14 , 3 , None , 2 , 6 ]})

 
# Find the mean absolute deviation
# skip Na values ​​when finding insane value

df.mad (axis = 1 , skipna = True )

Output:

Get Solution for free from DataCamp guru