Q. Consider the Data Frame mk, write a program to calculate average marks for each section after filling up of missing values.


DataFrame mk :-
          A      B     C     D
Acct   NaN   94.0  92.0  97.0
Eco   90.0   94.0   NaN  97.0
Eng   95.0   89.0  91.0  89.0
IP    94.0    NaN  99.0  95.0
Math  97.0  100.0  99.0   NaN

Answer :-

import pandas as pd
import numpy as np

x = { 'A': {'Acct': np.NaN, 'Eco' : 90.0, 'Eng': 95.0,\
                   'IP' : 94.0, 'Math': 97.0},\
          'B' : { 'Acct': 94.0, 'Eco':94, 'Eng': 89,\
                      'IP' : np.NaN, 'Math' : 100},\
          'C': {'Acct': 92, 'Eco' : np.NaN, 'Eng' : 91,\
                     'IP' :99, 'Math':99},\
          'D': {'Acct':97, 'Eco': 97, 'Eng': 89,\
                     'IP' : 95, 'Math': np.NaN}}

mk = pd.DataFrame (x)
print ("DataFrame mk :-\n", mk)

nmk = mk.fillna( {'A':20, 'B':10, 'C':20, 'D':0})
print("\nAverage marks section wise after filling missing values\n")
print (nmk.mean())


Output :-

 DataFrame mk :-
          A      B     C     D
Acct   NaN   94.0  92.0  97.0
Eco   90.0   94.0   NaN  97.0
Eng   95.0   89.0  91.0  89.0
IP    94.0    NaN  99.0  95.0
Math  97.0  100.0  99.0   NaN

Average marks section wise after filling missing values

A    79.2
B    77.4
C    80.2
D    75.6
dtype: float64

>>>

Post a Comment

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

Previous Post Next Post