Informatics Practices
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)
Python Pandas
1 Like
Answer
(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
Working
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
Working
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
Working
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
Working
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.
Answered By
3 Likes
Related Questions
Assume that required libraries (Pandas and Numpy) are imported and DataFrame ndf has been created as shown in solved problem 16. Predict the output produced by following code fragment :
print(ndf[ndf["age"] > 30]) print(ndf.head(2)) print(ndf.tail(3))The following DataFrame
ndfis from solved problem 16 :Name Sex Position City age Projects Budget 0 Rabina F Manager Bangalore 30 13 48 1 Evan M Programer New Delhi 27 17 13 2 Jia F Manager Chennai 32 16 32 3 Lalit M Manager Mumbai 40 20 21 4 Jaspreet M Programmer Chennai 28 21 17 5 Suji F Programmer Bangalore 32 14 10Given the two DataFrames as :
>>> dfc1 - - 0 1 0 2 a 1 3 b 2 4 c>>> dfc2 - - 0 1 2 0 2 3 4 2 p q rWhy are following statements giving errors ?
(a) print(dfc1 + dfc2)
(b) print(dfc1.sub(dfc2))
(c) print(dfc1 * dfc2)
Consider the DataFrame wdf as shown below :
minTemp maxTemp Rainfall Evaporation 0 2.9 8.0 24.3 0.0 1 3.1 14.0 26.9 3.6 2 6.2 13.7 23.4 3.6 3 5.3 13.3 15.5 39.8 4 6.3 17.6 16.1 2.8 5 5.4 18.2 16.9 0.0 6 5.5 21.1 18.2 0.2 7 4.8 18.3 17.0 0.0 8 3.6 20.8 19.5 0.0 9 7.7 19.4 22.8 16.2 10 9.9 24.1 25.2 0.0 11 11.8 28.5 27.3 0.2 12 13.2 29.1 27.9 0.0 13 16.8 24.1 30.9 0.0 14 19.4 28.1 31.2 0.0 15 21.6 34.4 32.1 0.0 16 20.4 33.8 31.2 0.0 17 18.5 26.7 30.0 1.2 18 18.8 32.4 32.3 0.6 19 17.6 28.6 33.4 0.0 20 19.7 30.3 33.4 0.0 (a) Write statement(s) to calculate minimum value for each of the columns.
(b) Write statement(s) to calculate maximum value for each of the rows.
(c) Write statement(s) to calculate variance for column Rainfall.
(d) Write statement(s) to compute mean , mode median for last 10 rows.
Write a program to print a DataFrame one column at a time and print only first three columns.