CBSE Class 12 Informatics Practices
Question 151 of 167
Python Pandas — I — Question 5
Back to all questions 5
Question Ekam, a Data Analyst with a multinational brand has designed the DataFrame df that contains the four quarters' sales data of different stores as shown below :
| Store | Qtr1 | Qtr2 | Qtr3 | Qtr4 | |
|---|---|---|---|---|---|
| 0 | Store1 | 300 | 240 | 450 | 230 |
| 1 | Store2 | 350 | 340 | 403 | 210 |
| 2 | Store3 | 250 | 180 | 145 | 160 |
Answer the following questions :
(i) Predict the output of the following Python statement :
(a) print(df.size)
(b) print(df[1:3])
(ii) Delete the last row from the DataFrame.
(iii) Write Python statement to add a new column Total_Sales which is the addition of all the 4 quarter sales.
(i)
(a) print(df.size)
15
The size attribute of a DataFrame returns the total number of elements in the DataFrame df.
(b) print(df[1:3])
Store Qtr1 Qtr2 Qtr3 Qtr4
1 Store2 350 340 403 210
2 Store3 250 180 145 160
This statement uses slicing to extract rows 1 and 2 from the DataFrame df.
(ii)
df = df.drop(2) Store Qtr1 Qtr2 Qtr3 Qtr4
0 Store1 300 240 450 230
1 Store2 350 340 403 210
(iii)
df['Total_Sales'] = df['Qtr1'] + df['Qtr2'] + df['Qtr3'] + df['Qtr4'] Store Qtr1 Qtr2 Qtr3 Qtr4 Total_Sales
0 Store1 300 240 450 230 1220
1 Store2 350 340 403 210 1303
2 Store3 250 180 145 160 735