Anjeev Singh Academy

Class 12 IP 065 Ch 1 Python Pandas I Type A Very Short Questions Answer

7. Consider tow objects x and y. x is a list whereas y is a Series. Both have values 20, 40, 90, 110.

What will be the output of following two statements considering that the above objects have been created already ?

(i)  print ( x * 2)                                  (ii) print (y * 2)

Justify your answer.    [CBSE Sample Paper 20-21]

 Ans : Output

(i) print( x * 2)

[20, 40, 90, 110, 20, 40, 90, 110]

(ii) print( y * 2)

 
040
180
2180
3220

Reason:

  • List does not support vectorized operation, while Series support vectorized operation.
  • * is work as replication operator with the List while * is work as multiplication operator with Series.

8. Given a dataframe df as shown below

 ABD
0151719
1161820
2202122

What will be the Result of following code statements?

(a) df [‘C’] = np.NaN                 (b) df [‘C’]  = [2, 5]   

(c) df[‘C’]= [12, 15, 27]

Ans:  Creating a DataFrame:

df = pd.DataFrame({‘A’: [15,16,20],   ‘B’:[17,18,21],   ‘D’:[19,20,22]})

(a) df[‘C’] = np.NaN               # It will add one column ‘C’ with NaN value

 df

    A   B   D   C

0  15  17  19 NaN

1  16  18  20 NaN

2  20  21  22 NaN

(b) df[‘C’]=[2,5]

Its raise a ValueError “Length of values does not match length of index”

(c) df[‘C’]= [12,15,27]

df

    A   B   D   C

0  15  17  19  12

1  16  18  20  15

2  20  21  22  27

            It will update the value of column ‘C’.

9. Write code statements to list the following, from a dataframe namely sales.

(a) List only columns ‘Item’ and ‘Revenue’.

(b) List rows from 3 to 7.

(c) List the value of cell in 5th row, ‘Item’ column.

Ans:

(a) Sales[[‘Item’, ‘Revenue’]]

(b) Sales.iloc[2:7]      

# 3rd Row means 2nd Index and 7th Row means 6th Index position

(c) Sales.Item[5]  

or

Sales.at[4, ‘Item’]            

# 5th row means 4th index position

10. Hitesh wants to display the last four rows of the dataframe df and has written the following code:

df.tail( )

But last 5 rows are being displayed. Identify the error and rewrite the correct code so that last 4 rows get displayed.  [CBSE Sample Paper 2019-20]

Ans: tail( ) function by default returns the last / bottom five rows, if you have not given any integer argument.

Use df.tail(4) , to display the last four rows of the dataframe.

Sorry! You cannot copy content of this page. Please contact, in case you want this content.

Scroll to Top