CBSE Class 12 Informatics Practices Question 90 of 101

Python Pandas — II — Question 14

Back to all questions
14
Question

Question 14

Consider the following code that creates two DataFrames :

ore1 = pd.DataFrame(np.array([[20, 35, 25, 20], [11, 28, 32, 29]]),
       columns = ['iron', 'magnesium', 'copper', 'silver'])
ore2 = pd.DataFrame(np.array([[14, 34, 26, 26], [33, 19, 25, 23]]),
       columns = ['iron', 'magnesium', 'gold', 'silver'])

What will be the output produced by the following code fragments ?

(a) print(ore1 + ore2)
     ore3 = ore1.radd(ore2)
     print(ore3)

(b) print(ore1 - ore2)
     ore3 = ore1.rsub(ore2)
     print(ore3)

(c) print(ore1 * ore2)
     ore3 = ore1.mul(ore2)
     print(ore3)

(d) print(ore1 / ore2)
     ore3 = ore1.rdiv(ore2)
     print(ore3)

Answer

(a)

Output
   copper  gold  iron  magnesium  silver
0     NaN   NaN    34         69      46
1     NaN   NaN    44         47      52
   copper  gold  iron  magnesium  silver
0     NaN   NaN    34         69      46
1     NaN   NaN    44         47      52
Explanation
  1. print(ore1 + ore2): This line attempts to add the DataFrames ore1 and ore2 using the '+' operator. When adding DataFrames with different shapes and column names, pandas aligns the DataFrames based on indices and columns, resulting in NaN values where elements are missing in either DataFrame.
  2. ore3 = ore1.radd(ore2): This line uses the radd() method, which reverses the addition operation between DataFrames ore1 and ore2.
  3. The radd() function and '+' operator produce the same result in pandas, as the order of operands does not affect addition due to its commutative property.

(b)

Output
  copper  gold  iron  magnesium  silver
0     NaN   NaN     6          1      -6
1     NaN   NaN   -22          9       6
   copper  gold  iron  magnesium  silver
0     NaN   NaN    -6         -1       6
1     NaN   NaN    22         -9      -6
Explanation
  1. print(ore1 - ore2): This line performs a subtraction operation between corresponding elements in DataFrames ore1 and ore2. When subtracting DataFrames with different shapes and column names, pandas aligns the DataFrames based on indices and columns, resulting in NaN values where elements are missing in either DataFrame.
  2. ore3 = ore1.rsub(ore2): This line uses the rsub() method, which reverses the subtraction operation between DataFrames ore1 and ore2.
  3. The rsub() function and the '-' operator in pandas do not produce the same result because the rsub() function performs reverse subtraction, which means it subtracts the left operand from the right, while the '-' subtracts the right operand from the left operand.

(c)

Output
   copper  gold  iron  magnesium  silver
0     NaN   NaN   280       1190     520
1     NaN   NaN   363        532     667
   copper  gold  iron  magnesium  silver
0     NaN   NaN   280       1190     520
1     NaN   NaN   363        532     667
Explanation
  1. print(ore1 * ore2): This line attempts to perform element-wise multiplication between the DataFrames ore1 and ore2 using the '*' operator. When multiplying DataFrames with different shapes and column names, pandas aligns the DataFrames based on indices and columns, resulting in NaN values where elements are missing in either DataFrame.
  2. ore3 = ore1.mul(ore2): This line uses the mul() method to perform element-wise multiplication between DataFrames ore1 and ore2.

(d)

Output
   copper  gold      iron  magnesium    silver
0     NaN   NaN  1.428571   1.029412  0.769231
1     NaN   NaN  0.333333   1.473684  1.260870
   copper  gold  iron  magnesium    silver
0     NaN   NaN   0.7   0.971429  1.300000
1     NaN   NaN   3.0   0.678571  0.793103
Explanation
  1. print(ore1 / ore2): This line attempts to perform element-wise division between the DataFrames ore1 and ore2 using the '/' operator. When dividing DataFrames with different shapes and column names, pandas aligns the DataFrames based on indices and columns, resulting in NaN values where elements are missing in either DataFrame.
  2. ore3 = ore1.rdiv(ore2): This line uses the rdiv() method to perform reciprocal division between DataFrames ore1 and ore2.