Pandas DataFrame Mad Method

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn how to use the mad() method in the Pandas library to calculate the mean absolute deviation of a DataFrame. The mean absolute deviation is a measure of the variation in a dataset, defined as the average distance between each data value and the mean.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/DataStructuresGroup -.-> python/dictionaries("`Dictionaries`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("`Data Analysis`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/lists -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/tuples -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/dictionaries -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/importing_modules -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/numerical_computing -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/data_analysis -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} python/build_in_functions -.-> lab-68656{{"`Pandas DataFrame Mad Method`"}} end

Import the necessary libraries

First, import the pandas library as pd. This will allow you to use the Pandas DataFrame and its mad() method.

import pandas as pd

Create a DataFrame

Next, create a DataFrame to work with. In this example, you will create a DataFrame with four columns (A, B, C, and D) and three rows.

df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9], "D": [10, 11, 12]})

Calculate the mean absolute deviation

Now that you have a DataFrame, you can calculate the mean absolute deviation using the mad() method. To calculate the mean absolute deviation over the index axis, specify axis=0 as an argument.

mad_index = df.mad(axis=0)

To calculate the mean absolute deviation over the column axis, specify axis=1 as an argument.

mad_columns = df.mad(axis=1)

Print the results

Finally, you can print the mean absolute deviation values.

print("Mean Absolute Deviation over index axis:")
print(mad_index)

print("Mean Absolute Deviation over column axis:")
print(mad_columns)

Summary

In this lab, you learned how to use the mad() method in the Pandas library to calculate the mean absolute deviation of a DataFrame. The mean absolute deviation is a measure of the variation in a dataset, defined as the average distance between each data value and the mean. By following the steps outlined in this lab, you should now be able to apply the mad() method to your own DataFrames and analyze the variation in your data.

Other Python Tutorials you may like