Informatics Practices
Given 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 r
Why are following statements giving errors ?
(a) print(dfc1 + dfc2)
(b) print(dfc1.sub(dfc2))
(c) print(dfc1 * dfc2)
Python Pandas
1 Like
Answer
(a) print(dfc1 + dfc2) — This statement tries to add two DataFrames dfc1 and dfc2. However, they have different shapes (dfc1 has 3 rows and 2 columns, while dfc2 has 2 rows and 3 columns), and their indices do not match. DataFrame addition requires both DataFrames to have the same shape and compatible indices, which is not the case here.
(b) print(dfc1.sub(dfc2)) — This statement attempts to subtract dfc1 from dfc2 using the sub() method. Similar to addition, subtraction between DataFrames requires them to have the same shape and compatible indices, which is not satisfied here due to the mismatch in shapes and indices.
(c) print(dfc1 * dfc2) — This statement tries to perform element-wise multiplication between dfc1 and dfc2. Again, this operation requires both DataFrames to have the same shape and compatible indices, which is not the case here due to the mismatched shapes and indices.
Answered By
1 Like
Related Questions
How do you create quantiles and quartiles in Python Pandas ?
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 10Consider 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)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.