
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Write a Python program to find the mean absolute deviation of rows and columns in a dataframe
Solution
Assume you have a dataframe and mean absolute deviation of rows and column is,
mad of columns: Column1 0.938776 Column2 0.600000 dtype: float64 mad of rows: 0 0.500 1 0.900 2 0.650 3 0.900 4 0.750 5 0.575 6 1.325 dtype: float64
To solve this, we will follow the steps given below −
Define a dataframe
Calculate mean absolute deviation of row as,
df.mad()
Calculate mean absolute deviation of row as,
df.mad(axis=1)
Example
Let’s see the following code to get a better understanding −
import pandas as pd data = {"Column1":[6, 5.3, 5.9, 7.8, 7.6, 7.45, 7.75], "Column2":[7, 7.1, 7.2, 6, 6.1, 6.3, 5.1]} df = pd.DataFrame(data) print("DataFrame is:\n",df) print("mad of columns:\n",df.mad()) print("mad of rows:\n",df.mad(axis=1))
Output
DataFrame is: Column1 Column2 0 6.00 7.0 1 5.30 7.1 2 5.90 7.2 3 7.80 6.0 4 7.60 6.1 5 7.45 6.3 6 7.75 5.1 mad of columns: Column1 0.938776 Column2 0.600000 dtype: float64 mad of rows: 0 0.500 1 0.900 2 0.650 3 0.900 4 0.750 5 0.575 6 1.325 dtype: float64
- Related Articles
- Absolute Deviation and Absolute Mean Deviation using NumPy
- Program for Mean Absolute Deviation in C++
- How to find the standard deviation of specific columns in a dataframe in Pandas Python?
- Write a Python function which accepts DataFrame Age, Salary columns second, third and fourth rows as input and find the mean, product of values
- Python Pandas – Count the rows and columns in a DataFrame
- Write a program in Python to transpose the index and columns in a given DataFrame
- Write a program in Python to remove first duplicate rows in a given dataframe
- Program to find matrix for which rows and columns holding sum of behind rows and columns in Python
- Write a program in Python to print dataframe rows as orderDict with a list of tuple values
- Write a program in Python to select any random odd index rows in a given DataFrame
- Write a program in Python to find the minimum rank of a particular column in a dataframe
- Write a program in Python to remove one or more than one columns in a given DataFrame
- How to get the mean of columns that contains numeric values of a dataframe in Pandas Python?
- Python - Calculate the standard deviation of a column in a Pandas DataFrame
- Write a Python code to swap last two rows in a given dataframe

Advertisements