• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / pandas mad – Calculate Mean Absolute Deviation in Python

pandas mad – Calculate Mean Absolute Deviation in Python

January 13, 2022 Leave a Comment

To find the mean absolute deviation of a series or a column in a DataFrame in pandas, the easiest way is to use the pandas mad() function.

df["Column1"].mad()

When doing data analysis, the ability to compute different summary statistics, such as the mean or standard deviation of a variable, is very useful to help us understand the data. One such summary statistic which can be useful is the mean absolute deviation of a variable.

The mean absolute deviation of a variable is computed as the mean of absolute deviation of data points from their mean.

Finding the mean absolute deviation of columns or a Series using pandas is easy. We can use the pandas mad() function to find the mean absolute deviation of a column of numbers.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 160.20, 209.45, 150.35, 187.52, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42] })

print(df)
# Output: 
    Name  Weight  Height
0    Jim  160.20   50.10
1  Sally  160.20   68.94
2    Bob  209.45   71.42
3    Sue  150.35   48.56
4   Jill  187.52   59.37
5  Larry  187.52   63.42

To get the mean absolute deviation of all columns in our DataFrame, we can use the pandas mad() function on the DataFrame in the following Python code:

print(df.mad())

# Output:
Weight    18.956667
Height     7.625000
dtype: float64

If we only want to get the mean absolute deviation of the column “Height”, we can do so easily like in the following Python code:

print(df["Height"].mad())

# Output:
7.625

Hopefully this article has been helpful for you to understand how to find the mean absolute deviation of a variable within a column or Series using the pandas mad() function in Python.

Other Articles You'll Also Like:

  • 1.  Get Name of Function in Python
  • 2.  Print Approximately Equal Symbol in Python
  • 3.  Using Python to Create List of Prime Numbers
  • 4.  Get Difference Between datetime Variables in Python
  • 5.  Cartesian Product of Two Lists in Python
  • 6.  pi in Python – Using Math Module and Leibniz Formula to Get Value of pi
  • 7.  Using Python to Count Number of False in List
  • 8.  Python Check if Object Has Attribute
  • 9.  pandas cumprod – Find Cumulative Product of Series or DataFrame
  • 10.  Creating a List of Zeros in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy