Informatics Practices
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.
Python Pandas
3 Likes
Answer
(a)
>>> wdf.min()
Output
Minimum values for each column:
minTemp 2.9
maxTemp 8.0
Rainfall 15.5
Evaporation 0.0
dtype: float64
(b)
>>> wdf.max(axis=1)
Output
Maximum values for each row:
0 24.3
1 26.9
2 23.4
3 39.8
4 17.6
5 18.2
6 21.1
7 18.3
8 20.8
9 22.8
10 25.2
11 28.5
12 29.1
13 30.9
14 31.2
15 34.4
16 33.8
17 30.0
18 32.4
19 33.4
20 33.4
dtype: float64
(c)
>>> wdf['Rainfall'].var()
Output
Variance for column Rainfall: 38.852999999999994
(d)
>>> last_10_rows = wdf.tail(10)
>>> last_10_rows.mean()
>>> last_10_rows.mode()
>>> last_10_rows.median()
Output
Mean for last 10 rows:
minTemp 17.78
maxTemp 29.60
Rainfall 30.97
Evaporation 0.20
dtype: float64
Mode for last 10 rows:
minTemp maxTemp Rainfall Evaporation
0 11.8 24.1 31.2 0.0
1 13.2 26.7 33.4 NaN
2 16.8 28.1 NaN NaN
3 17.6 28.5 NaN NaN
4 18.5 28.6 NaN NaN
5 18.8 29.1 NaN NaN
6 19.4 30.3 NaN NaN
7 19.7 32.4 NaN NaN
8 20.4 33.8 NaN NaN
9 21.6 34.4 NaN NaN
Median for last 10 rows:
minTemp 18.65
maxTemp 28.85
Rainfall 31.20
Evaporation 0.00
dtype: float64
Answered By
1 Like
Related Questions
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)Write a program to print a DataFrame one column at a time and print only first three columns.
Write a program to print a DataFrame one row at a time and print only first five rows.