Informatics Practices
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 ndf is 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 10
Python Pandas
1 Like
Answer
Name Sex Position City age Projects Budget
2 Jia F Manager Chennai 32 16 32
3 Lalit M Manager Mumbai 40 20 21
5 Suji F Programmer Bangalore 32 14 10
Name Sex Position City age Projects Budget
0 Rabina F Manager Bangalore 30 13 48
1 Evan M Programer New Delhi 27 17 13
Name Sex Position City age Projects Budget
3 Lalit M Manager Mumbai 40 20 21
4 Jaspreet M Programmer Chennai 28 21 17
5 Suji F Programmer Bangalore 32 14 10
Working
print(ndf[ndf["age"] > 30])— Thendf["age"] > 30creates a boolean mask where True indicates rows where the "age" column has values greater than 30. Thenndf[...]uses this boolean mask to filter the DataFramendf, returning only rows where the condition is True.print(ndf.head(2))— It prints the first 2 rows of the DataFramendf.print(ndf.tail(3))— It prints the last 3 rows of the DataFramendf.
Answered By
3 Likes
Related Questions
What is a quartile ? How is it different from quantile ?
How do you create quantiles and quartiles in Python Pandas ?
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 rWhy are following statements giving errors ?
(a) print(dfc1 + dfc2)
(b) print(dfc1.sub(dfc2))
(c) print(dfc1 * dfc2)
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)