Q. Consider Data Frame mk as shown below:


Data-frame 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

Write a program to fill missing values section wise as given below and name the new Data Frame as nmk :


For section 'A' fill 20,

For section 'B' fill 10,

For section 'C' fill 20,

For section 'D' fill 0


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("\nDataframe after filling missing values section wise\n")
print (nmk)


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

Dataframe after filling missing values section wise

         A      B     C     D
Acct  20.0   94.0  92.0  97.0
Eco   90.0   94.0  20.0  97.0
Eng   95.0   89.0  91.0  89.0
IP    94.0   10.0  99.0  95.0
Math  97.0  100.0  99.0   0.0

>>>

Post a Comment

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

Previous Post Next Post