# Hint: mean() on a DataFrame gives the result in a Serries. The axis of the function to be applied on can be set with axis={index (0), columns (1)}.
# To find the mean of the whole matrix, the mean method can be applied twice
print("Average temperature in Chur was: {}".format(t_chur_mean))
print("Average temperature in Spring was: {}".format(round(t_spring_mean,1)))
```
%%%% Output: stream
Average temperature in Chur was: 11.5
Average temperature in Spring was: 15.5
%% Cell type:markdown id: tags:
### .sort_values() Method:
- Sort the data based on the temperature in Zürich
- Sort the data based on decreasing temperature in Basel
%% Cell type:code id: tags:
``` python
data.sort_values("Zurich")
```
%%%% Output: execute_result
Luzern Bern Chur Zurich
Feb 5 6 1 0
Jan 2 5 -3 4
Mar 10 11 13 8
Apr 16 12 14 17
May 21 23 21 20
Jun 25 21 23 27
%% Cell type:code id: tags:
``` python
data.sort_values("Basel",ascending=False)
```
%%%% Output: execute_result
Luzern Bern Chur Zurich
May 21 23 21 20
Jun 25 21 23 27
Apr 16 12 14 17
Mar 10 11 13 8
Feb 5 6 1 0
Jan 2 5 -3 4
%% Cell type:markdown id: tags:
# Solution to Problem 4: Fuel Consumption
%% Cell type:code id: tags:
``` python
# First import Pandas
importpandasaspd
# load the database using pandas.read_csv with options: sep="," and index_col=0
# data = None
data=pd.read_csv("./data/d.fuel.dat")
data
```
%%%% Output: execute_result
X weight mpg type
0 1 2560 33 Small
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
5 6 2285 26 Small
6 7 2275 33 Small
7 8 2350 28 Small
8 9 2295 25 Small
9 10 1900 34 Small
10 11 2390 29 Small
11 12 2075 35 Small
12 13 2330 26 Small
13 14 3320 20 Sporty
14 15 2885 27 Sporty
15 16 3310 19 Sporty
16 17 2695 30 Sporty
17 18 2170 33 Sporty
18 19 2710 27 Sporty
19 20 2775 24 Sporty
20 21 2840 26 Sporty
21 22 2485 28 Sporty
22 23 2670 27 Compact
23 24 2640 23 Compact
24 25 2655 26 Compact
25 26 3065 25 Compact
26 27 2750 24 Compact
27 28 2920 26 Compact
28 29 2780 24 Compact
29 30 2745 25 Compact
30 31 3110 21 Compact
31 32 2920 21 Compact
32 33 2645 23 Compact
33 34 2575 24 Compact
34 35 2935 23 Compact
35 36 2920 27 Compact
36 37 2985 23 Compact
37 38 3265 20 Medium
38 39 2880 21 Medium
39 40 2975 22 Medium
40 41 3450 22 Medium
41 42 3145 22 Medium
42 43 3190 22 Medium
43 44 3610 23 Medium
44 45 2885 23 Medium
45 46 3480 21 Medium
46 47 3200 22 Medium
47 48 2765 21 Medium
48 49 3220 21 Medium
49 50 3480 23 Medium
50 51 3325 23 Large
51 52 3855 18 Large
52 53 3850 20 Large
53 54 3195 18 Van
54 55 3735 18 Van
55 56 3665 18 Van
56 57 3735 19 Van
57 58 3415 20 Van
58 59 3185 20 Van
59 60 3690 19 Van
%% Cell type:markdown id: tags:
To get a quick overview, we can view only the first 5 rows of the dataset. Print the first five rows using:
-**dataframe.loc**
-**DataFrame.head()**
%% Cell type:code id: tags:
``` python
# Print the first 5 rows using data.loc
data.loc[1:5,:]
```
%%%% Output: execute_result
X weight mpg type
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
5 6 2285 26 Small
%% Cell type:code id: tags:
``` python
# print the first 5 rows using data.head()
data.head()
```
%%%% Output: execute_result
X weight mpg type
0 1 2560 33 Small
1 2 2345 33 Small
2 3 1845 37 Small
3 4 2260 32 Small
4 5 2440 32 Small
%% Cell type:markdown id: tags:
### .mean() Method:
Now find :
- the average range of all cars
- the average range of all cars with type "Medium" (hint, select all rows with a certain constraint using **DataFrame[DataFrame[***column***].isin([***values***])]**
print("Average miles per galon is: \n{}".format(round(avg_mpg,2)),"\nAverage miles per galon for all Medium type cars is: \n{}".format(round(avg_medium,2)))
```
%%%% Output: stream
Average miles per galon is:
24.58
Average miles per galon for all Medium type cars is:
21.77
%% Cell type:markdown id: tags:
### Conversion to SI units
- Create a Series containing the range in km/l and another Series containing the weight in kg.
- Find the average of these new Vectors
%% Cell type:code id: tags:
``` python
t_kml=data["mpg"]*1.61/3.79
t_kg=data["weight"]*0.454
print(t_kml.head())
print(t_kg.head())
avg_kml=t_kml.mean()
avg_kg=t_kg.mean()
print("\nAverage Kilometer per liter is: \n{}".format(round(avg_kml,2)),"\nAverage weight in kilogram is: \n{}".format(round(avg_kg,2)))