Q. Consider the crop statistics Data Frame prodf. Write a program to calculate mean absolute deviation for all the crops.


DataFrame prodf :-

              Rice    Wheat  Pulses    Fruits
Andhra P.   7452.4      NaN   931.0    7830.0
Gujarat     1930.0   2737.0   818.0   11950.0
Kerala      2604.8      NaN     1.7     113.1
Punjab     11586.2  16440.5    33.0    7152.0
Tripura      814.6      0.5    23.2      44.1
Uttar P.   13754.0  30056.0  2184.4  140169.2

Answer :-

import pandas as pd
import numpy as np

iprod = { 'Rice': {'Andhra P.':7452.4, 'Gujarat' :1930.0, 'Kerala': 2604.8,\
                   'Punjab':11586.2, 'Tripura':814.6, 'Uttar P.':13754.0},\
          'Wheat' : { 'Andhra P.': np.NaN, 'Gujarat':2737.0, 'Kerala':np.NaN,\
                      'Punjab' :16440.5, 'Tripura':0.5, 'Uttar P.':30056.0},\
          'Pulses': {'Andhra P.':931.0, 'Gujarat':818.0, 'Kerala':1.7,\
                     'Punjab' :33.0, 'Tripura':23.2, 'Uttar P.':2184.4},\
          'Fruits': {'Andhra P.':7830.0, 'Gujarat':11950.0, 'Kerala':113.1,\
                     'Punjab' :7152.0, 'Tripura':44.1, 'Uttar P.':140169.2}}

prodf = pd.DataFrame(iprod)
print ("DataFrame prodf :-\n")
print (prodf)

print("\nMean Absolute Deviation Crop wise:-\n")
print (prodf.mad())


Output :-

 DataFrame prodf :-

              Rice    Wheat  Pulses    Fruits
Andhra P.   7452.4      NaN   931.0    7830.0
Gujarat     1930.0   2737.0   818.0   11950.0
Kerala      2604.8      NaN     1.7     113.1
Punjab     11586.2  16440.5    33.0    7152.0
Tripura      814.6      0.5    23.2      44.1
Uttar P.   13754.0  30056.0  2184.4  140169.2

Mean Absolute Deviation Crop wise:-

Rice       4573.866667
Wheat     10939.750000
Pulses      645.916667
Fruits    37430.933333
dtype: float64

>>>

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post