# Class 12 IP 065 Ch 1 Python Pandas 1 Type C Long Answer Questions

### 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):

(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â€™]