Programming Problems (Type C)
4. Series objects Templ, Temp2, Tcmp3, Temp4 store the temperatures of days of week1, week2, week3, week4 respectively. Write a script to
(a) print the average temperature per week.
(b) print average temperature of entire month
Ans:
import pandas as pd
import numpy as np
week1 = np.random.randint(12, 30, 7)
week2 = np.random.randint(12, 30, 7)
week3 = np.random.randint(12, 30, 7)
week4 = np.random.randint(12, 30, 7)
Temp1 = pd.Series(week1, index=range(1,8))
Temp2 = pd.Series(week2, index=range(1,8))
Temp3 = pd.Series(week3, index=range(1,8))
Temp4 = pd.Series(week4, index=range(1,8))
print(“(a) Average Temperature per week”)
avgTemp1 = Temp1.sum()/7
avgTemp2 = Temp2.sum()/7
avgTemp3 = Temp3.sum()/7
avgTemp4 = Temp4.sum()/7
print(“Week1 : {:.2f}”.format(avgTemp1))
print(“Week2 : {:.2f}”.format(avgTemp2))
print(“Week3 : {:.2f}”.format(avgTemp3))
print(“Week4 : {:.2f}”.format(avgTemp4))
print(“(b)Average Temperature of Entire month”)
monthTemp = Temp1.sum() + Temp2.sum() + Temp3.sum()+Temp4.sum()
monthAvgTemp = monthTemp / 28
print(“Monthly Average : {:.2f}”.format(monthAvgTemp))
5. Consider the following DataFrame df and answer any four questions from (i) – (v):
rollno | name | UT1 | UT2 | UT3 | UT4 |
1 2 3 4 5 6 | Prerna Singh Manish Arora Tanish Goel Falguni Jain Kanika Bhatnagar Ramandeep Kaur | 24 18 20 22 15 20 | 24 17 22 20 20 15 | 20 19 18 24 18 22 | 22 22 24 20 22 24 |
(i) Write down the command that will give the following output:
rollno 6
name Tanish Goel
UT1 24
UT2 24
UT3 24
UT4 24
(a) print(df.max)
(b) print(df.max())
(c) print(df.max(axis=1))
d) print(df.max, axis = 1)
Ans: (b) print(df.max())
(ii) The teacher needs to know the marks scored by the student with roll number 4. Help her identify the correct set of statement(s) from the given options:
(a) df1 = df[df[‘rollno’] == 4]
(b) df1 = df[rollno == 4]
print(df1)
print(df1)
(c) df1 = df[df.rollno = 4]
print(df1)
(d) df1 = df[df.rollno == 4]
print(df1)
Ans: (a) and (d)
(iii) Which of the following statement(s) will give the exact number of values in each column of the dataframe?
(I) print(df.count( ))
(II) print(df.count(0))
(III) print(df.count)
(iv) print(df.count(axis = ‘index’))
Choose the correct option:
(a) both (I) and (II) (b) only (II)
(c) (I), (II) and (III) (d) (I), (II) and (IV)
Ans: (d) (I), (II) and (IV)
(iv) Which of the following command will display the column labels of the DataFrame?
(a) print(df.columns())
(b) print(df.columns())
(c) pirnt(df.column)
(d) print(df.columns)
Ans: (d) print(df.columns)
(v) Ms. Sharma the class teacher wants to add a new column, the scores of Grade with the values, ‘A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’, to the DataFrame. Help her choose the command to do so:
(a) df.column = [‘A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’]
(b) df[‘Grade’] = [‘A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’]
(c) df.loc[‘Grade’] = [‘A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’]
(d) Both (b) and (c) are correct
[CBSE Sample Paper 20-21]
Ans: (b) df[‘Grade’] = [‘A’, ‘B’, ‘A’, ‘A’, ‘B’, ‘A’]