Pandas DataFrame mad() Method
The variation in any dataset can be determined by the mean absolute deviation(MAD) and it can be defined as the average distance between each data value and the mean. In this tutorial, we will learn the Python pandas DataFrame.mad()
method. When the DataFrame.mad()
method applied on the DataFrame, it returns the mean absolute deviation of the values over the requested axis
.
The below shows the syntax of the DataFrame.mad()
method.
Syntax
DataFrame.mad(axis=None, skipna=None, level=None)
Parameters
axis: '0' represents the index and '1' represents the columns. When the axis=0
, method applied over the index
axis and when the axis=1
method applied over the column
axis.
skipna: It represents the bool(True or False). The default value is None. If this parameter is True
, it excludes all NA/null values when computing the result.
level: It represents the int or level name, the default value is None. It counts along with the particular level, if the DataFrame is Multiindex, collapsing into a Series.
Example: The DataFrame.mad()
Method
Let's create a DataFrame and get the mean absolute deviation of the values over the index
axis by assigning parameter axis=0
in the DataFrame.mad()
method. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6],"C":[7,8,9],"D":[10,11,12]})
print("----------The DataFrame is------")
print(df)
print("---The mean absolute deviation of the DataFrame is---")
print(df.mad(axis=0))
----------The DataFrame is------
A B C D
0 1 4 7 10
1 2 5 8 11
2 3 6 9 12
---The mean absolute deviation of the DataFrame is---
A 0.666667
B 0.666667
C 0.666667
D 0.666667
dtype: float64
Example: The DataFrame.mad()
method along the column
axis
Let's create a DataFrame and get the mean absolute deviation of the values over the column axis by assigning parameter axis=1
in the DataFrame.mad()
method. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,2,3],"B":[4,5,6],"C":[7,8,9],"D":[10,11,12]})
print("----------The DataFrame is------")
print(df)
print("---The mean absolute deviation of the DataFrame is---")
print(df.mad(axis=1))
----------The DataFrame is------
A B C D
0 1 4 7 10
1 2 5 8 11
2 3 6 9 12
---The mean absolute deviation of the DataFrame is---
0 3.0
1 3.0
2 3.0
dtype: float64
Example: The DataFrame.mad()
Method excluding null values
Let's create a DataFrame with null values and get the mean absolute deviation of the values over the index axis excluding null values by passing parameter skipna
in the DataFrame.mad()
method. It excludes all NA/null values when computing the results. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A":[1,None,3],"B":[None,5,6],"C":[7,8,9],"D":[10,11,None]})
print("----------The DataFrame is------")
print(df)
print("---The mean absolute deviation of the DataFrame is---")
print(df.mad(axis=0,skipna=True))
----------The DataFrame is------
A B C D
0 1.0 NaN 7 10.0
1 NaN 5.0 8 11.0
2 3.0 6.0 9 NaN
---The mean absolute deviation of the DataFrame is---
A 1.000000
B 0.500000
C 0.666667
D 0.500000
dtype: float64
Conclusion
In this tutorial, we learned the Python pandas DataFrame.mad()
method. We learned the syntax, parameters and applying this method on the DataFrame to understand the DataFrame.mad()
method.