Q. Consider the following data-frame, and answer the questions given below:


import pandas as pd

df = pd.DataFrame({ 

"Quarter1": [2000, 4000, 5000, 4400, 10000],
 "Quarter2": [5800, 2500, 5400, 3000, 2900],
"Quarter3": [20000, 16000, 7000, 3600, 8200],
"Quarter4": [1400, 3700, 1700, 2000, 6000]})


(i) Write the code to find mean value from above dataframe df over the index and column axis.

(ii) Use sum() function to find the sum of all the values over the index axis.

(iii) Find the median of the dataframe df.


Answer :-

(i)

import pandas as pd

df = pd.DataFrame({ "Quarter1": [2000, 4000, 5000, 4400, 10000],
                    "Quarter2": [5800, 2500, 5400, 3000, 2900],
                    "Quarter3": [20000, 16000, 7000, 3600, 8200],
                    "Quarter4": [1400, 3700, 1700, 2000, 6000]})

print (df.mean(axis = 1))
print (df.mean(axis = 0))


Output :-

 0    7300.0
1    6550.0
2    4775.0
3    3250.0
4    6775.0
dtype: float64
Quarter1     5080.0
Quarter2     3920.0
Quarter3    10960.0
Quarter4     2960.0
dtype: float64

>>>

 (ii)

import pandas as pd

df = pd.DataFrame({ "Quarter1": [2000, 4000, 5000, 4400, 10000],
                    "Quarter2": [5800, 2500, 5400, 3000, 2900],
                    "Quarter3": [20000, 16000, 7000, 3600, 8200],
                    "Quarter4": [1400, 3700, 1700, 2000, 6000]})

print (df.sum(axis = 1))


Output :-

0    29200
1    26200
2    19100
3    13000
4    27100
dtype: int64

>>>


(iii)

import pandas as pd

df = pd.DataFrame({ "Quarter1": [2000, 4000, 5000, 4400, 10000],
                    "Quarter2": [5800, 2500, 5400, 3000, 2900],
                    "Quarter3": [20000, 16000, 7000, 3600, 8200],
                    "Quarter4": [1400, 3700, 1700, 2000, 6000]})

print (df.median())


Output :-

Quarter1    4400.0
Quarter2    3000.0
Quarter3    8200.0
Quarter4    2000.0
dtype: float64

>>>

Post a Comment

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

Previous Post Next Post