Skip to content
Snippets Groups Projects
Commit 8a3589e2 authored by msyamkumar's avatar msyamkumar
Browse files

Lecture 33 worksheet

parent 378ce0ce
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import pandas as pd
from pandas import Series, DataFrame
import sqlite3
```
%% Cell type:code id: tags:
``` python
c = sqlite3.connect("bus.db")
```
%% Cell type:code id: tags:
``` python
def qry(sql):
return pd.read_sql(sql, c)
```
%% Cell type:markdown id: tags:
# How many people use the bus system daily?
%% Cell type:code id: tags:
``` python
qry("SELECT SUM(DailyBoardings) FROM boarding")
```
%% Output
SUM(DailyBoardings)
0 55987.18
%% Cell type:markdown id: tags:
# How many stops does each route have?
%% Cell type:code id: tags:
``` python
qry("SELECT Route, COUNT() AS tot FROM boarding GROUP BY Route")
```
%% Output
Route tot
0 1 32
1 2 125
2 3 150
3 4 114
4 5 91
5 6 211
6 10 130
7 11 93
8 12 69
9 13 56
10 14 133
11 15 173
12 16 78
13 17 15
14 18 90
15 19 100
16 20 52
17 21 38
18 22 36
19 25 21
20 26 33
21 27 96
22 28 97
23 29 50
24 30 48
25 31 51
26 32 60
27 33 65
28 34 28
29 35 66
30 36 11
31 37 95
32 38 159
33 39 32
34 40 42
35 44 59
36 47 78
37 48 23
38 49 34
39 50 33
40 51 36
41 52 19
42 55 12
43 56 130
44 57 118
45 58 88
46 67 27
47 70 127
48 71 102
49 72 104
50 73 85
51 75 39
52 80 47
53 81 32
54 82 33
55 84 6
%% Cell type:markdown id: tags:
# Which routes have less than 20 stops?
%% Cell type:code id: tags:
``` python
qry("SELECT Route, COUNT() AS tot FROM boarding GROUP BY Route HAVING tot < 20")
```
%% Output
Route tot
0 17 15
1 36 11
2 52 19
3 55 12
4 84 6
%% Cell type:markdown id: tags:
# How many people board each route daily?
Show most popular routes first.
%% Cell type:code id: tags:
``` python
qry("SELECT Route, SUM(DailyBoardings) AS tot FROM boarding GROUP BY Route ORDER BY tot DESC")
```
%% Output
Route tot
0 80 10211.79
1 2 4808.03
2 6 4537.02
3 10 4425.23
4 3 2708.55
5 4 2656.99
6 15 2179.98
7 38 1955.85
8 28 1868.31
9 5 1634.69
10 14 1373.81
11 16 1258.93
12 18 1039.57
13 22 995.21
14 19 827.53
15 50 748.75
16 67 729.54
17 70 710.80
18 30 687.13
19 72 636.95
20 13 615.20
21 40 602.92
22 21 590.86
23 20 545.91
24 71 497.09
25 56 477.44
26 57 464.86
27 73 448.87
28 75 435.35
29 44 416.90
30 11 392.43
31 47 379.89
32 81 371.76
33 58 362.59
34 12 329.51
35 37 319.82
36 27 298.07
37 17 294.55
38 82 219.48
39 33 206.53
40 1 181.44
41 52 176.24
42 39 140.89
43 35 140.42
44 31 139.87
45 51 137.57
46 55 129.23
47 84 114.21
48 29 111.28
49 26 107.10
50 32 86.47
51 34 81.97
52 49 61.83
53 36 59.13
54 48 30.65
55 25 24.19
%% Cell type:markdown id: tags:
# Which bus routes have at least 3 stops with more than 50 daily boardings?
%% Cell type:code id: tags:
``` python
qry("SELECT Route, COUNT() as stops FROM boarding WHERE DailyBoardings > 50 GROUP BY Route HAVING stops > 3")
```
%% Output
Route stops
0 2 30
1 3 18
2 4 9
3 5 6
4 6 24
5 10 25
6 14 4
7 15 10
8 16 4
9 22 4
10 28 12
11 38 10
12 80 36
%% Cell type:markdown id: tags:
# Which bus routes have at least 10 stops with more than 50 daily boardings?
%% Cell type:code id: tags:
``` python
qry("SELECT Route, COUNT() as stops FROM boarding WHERE DailyBoardings > 50 GROUP BY Route HAVING stops > 10")
```
%% Output
Route stops
0 2 30
1 3 18
2 6 24
3 10 25
4 28 12
5 80 36
%% Cell type:markdown id: tags:
# Which bus routes have at least 3 stops with more than 100 daily boardings?
%% Cell type:code id: tags:
``` python
qry("SELECT Route, COUNT() as stops FROM boarding WHERE DailyBoardings > 100 GROUP BY Route HAVING stops > 3")
```
%% Output
Route stops
0 2 14
1 6 8
2 10 12
3 80 25
%% Cell type:code id: tags:
``` python
```
File added
%% Cell type:code id: tags:
``` python
import pandas as pd
from pandas import Series, DataFrame
import sqlite3
```
%% Cell type:code id: tags:
``` python
c = sqlite3.connect("bus.db")
```
%% Cell type:code id: tags:
``` python
def qry(sql):
return pd.read_sql(sql, c)
```
%% Cell type:code id: tags:
``` python
qry("SELECT * FROM boarding").head()
```
%% Output
index StopID Route Lat Lon DailyBoardings
0 0 1163 27 43.073655 -89.385427 1.03
1 1 1163 47 43.073655 -89.385427 0.11
2 2 1163 75 43.073655 -89.385427 0.34
3 3 1164 6 43.106465 -89.340021 10.59
4 4 1167 3 43.077867 -89.369993 3.11
%% Cell type:markdown id: tags:
# How many people use the bus system daily?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# How many stops does each route have?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# Which routes have less than 20 stops?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# How many people board each route daily?
Show most popular routes first.
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# Which bus routes have at least 3 stops with more than 50 daily boardings?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# Which bus routes have at least 10 stops with more than 50 daily boardings?
%% Cell type:code id: tags:
``` python
```
%% Cell type:markdown id: tags:
# Which bus routes have at least 3 stops with more than 100 daily boardings?
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment