Q. Consider the crop statistics DataFrame prodf. Write a program to print the average crop production for each crop.


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("\nAverage crop production:-\n")
print (prodf.mean())


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

Average crop production:-

Rice       6357.000000
Wheat     12308.500000
Pulses      665.216667
Fruits    27876.400000
dtype: float64

>>>

Post a Comment

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

Previous Post Next Post