Q. Consider the crop statistics DataFrame prodf. Write a program to calculate mean absolute deviation for all the states.


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 State wise:-\n")
print (prodf.mad (axis = 1))


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 State wise:-

Andhra P.     2982.311111
Gujarat       3795.625000
Kerala        1132.177778
Punjab        5210.425000
Tripura        297.000000
Uttar P.     46814.150000
dtype: float64

>>>

Post a Comment

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

Previous Post Next Post