CBSE Class 12 Informatics Practices
Question 90 of 101
Python Pandas — II — Question 14
Back to all questions 14
Question 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)
(a)
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
print(ore1 + ore2): This line attempts to add the DataFramesore1andore2using 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.ore3 = ore1.radd(ore2): This line uses theradd()method, which reverses the addition operation between DataFramesore1andore2.- 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)
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
print(ore1 - ore2): This line performs a subtraction operation between corresponding elements in DataFramesore1andore2. 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.ore3 = ore1.rsub(ore2): This line uses thersub()method, which reverses the subtraction operation between DataFramesore1andore2.- The
rsub()function and the '-' operator in pandas do not produce the same result because thersub()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)
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
print(ore1 * ore2): This line attempts to perform element-wise multiplication between the DataFramesore1andore2using 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.ore3 = ore1.mul(ore2): This line uses themul()method to perform element-wise multiplication between DataFramesore1andore2.
(d)
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
print(ore1 / ore2): This line attempts to perform element-wise division between the DataFramesore1andore2using 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.ore3 = ore1.rdiv(ore2): This line uses therdiv()method to perform reciprocal division between DataFramesore1andore2.