Skip to content

Commit f56c9ab

Browse files
committed
lab 8 examples and slides
1 parent f7d7a55 commit f56c9ab

5 files changed

+3721
-1
lines changed

lab_07/duckdb_example.ipynb

+283-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,260 @@
1414
"cell_type": "code",
1515
"execution_count": 2,
1616
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
" Series Name Series Code \\\n",
23+
"0 Access to electricity (% of population) EG.ELC.ACCS.ZS \n",
24+
"1 Access to electricity (% of population) EG.ELC.ACCS.ZS \n",
25+
"2 Access to electricity (% of population) EG.ELC.ACCS.ZS \n",
26+
"3 Access to electricity (% of population) EG.ELC.ACCS.ZS \n",
27+
"4 Access to electricity (% of population) EG.ELC.ACCS.ZS \n",
28+
".. ... ... \n",
29+
"266 None None \n",
30+
"267 None None \n",
31+
"268 None None \n",
32+
"269 Data from database: World Development Indicators None \n",
33+
"270 Last Updated: 01/28/2025 None \n",
34+
"\n",
35+
" Country Name Country Code 1990 [YR1990] 2000 [YR2000] 2014 [YR2014] \\\n",
36+
"0 Afghanistan AFG .. 4.4 89.5 \n",
37+
"1 Albania ALB 100 99.4 100 \n",
38+
"2 Algeria DZA .. 98.6 99.3 \n",
39+
"3 American Samoa ASM .. .. .. \n",
40+
"4 Andorra AND 100 100 100 \n",
41+
".. ... ... ... ... ... \n",
42+
"266 None None None None None \n",
43+
"267 None None None None None \n",
44+
"268 None None None None None \n",
45+
"269 None None None None None \n",
46+
"270 None None None None None \n",
47+
"\n",
48+
" 2015 [YR2015] 2016 [YR2016] 2017 [YR2017] 2018 [YR2018] 2019 [YR2019] \\\n",
49+
"0 71.5 97.7 97.7 93.4 97.7 \n",
50+
"1 100 99.9 99.9 100 100 \n",
51+
"2 99.4 99.4 99.5 99.6 99.5 \n",
52+
"3 .. .. .. .. .. \n",
53+
"4 100 100 100 100 100 \n",
54+
".. ... ... ... ... ... \n",
55+
"266 None None None None None \n",
56+
"267 None None None None None \n",
57+
"268 None None None None None \n",
58+
"269 None None None None None \n",
59+
"270 None None None None None \n",
60+
"\n",
61+
" 2020 [YR2020] 2021 [YR2021] 2022 [YR2022] 2023 [YR2023] \n",
62+
"0 97.7 97.7 85.3 .. \n",
63+
"1 100 100 100 .. \n",
64+
"2 99.7 99.8 100 .. \n",
65+
"3 .. .. .. .. \n",
66+
"4 100 100 100 .. \n",
67+
".. ... ... ... ... \n",
68+
"266 None None None None \n",
69+
"267 None None None None \n",
70+
"268 None None None None \n",
71+
"269 None None None None \n",
72+
"270 None None None None \n",
73+
"\n",
74+
"[271 rows x 16 columns]\n"
75+
]
76+
}
77+
],
78+
"source": [
79+
"with duckdb.connect('world_bank.db') as con:\n",
80+
" df = con.table('electricity').to_df()\n",
81+
" print(df)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 3,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"data": {
91+
"text/html": [
92+
"<div>\n",
93+
"<style scoped>\n",
94+
" .dataframe tbody tr th:only-of-type {\n",
95+
" vertical-align: middle;\n",
96+
" }\n",
97+
"\n",
98+
" .dataframe tbody tr th {\n",
99+
" vertical-align: top;\n",
100+
" }\n",
101+
"\n",
102+
" .dataframe thead th {\n",
103+
" text-align: right;\n",
104+
" }\n",
105+
"</style>\n",
106+
"<table border=\"1\" class=\"dataframe\">\n",
107+
" <thead>\n",
108+
" <tr style=\"text-align: right;\">\n",
109+
" <th></th>\n",
110+
" <th>Series Name</th>\n",
111+
" <th>Series Code</th>\n",
112+
" <th>Country Name</th>\n",
113+
" <th>Country Code</th>\n",
114+
" <th>1990 [YR1990]</th>\n",
115+
" <th>2000 [YR2000]</th>\n",
116+
" <th>2014 [YR2014]</th>\n",
117+
" <th>2015 [YR2015]</th>\n",
118+
" <th>2016 [YR2016]</th>\n",
119+
" <th>2017 [YR2017]</th>\n",
120+
" <th>2018 [YR2018]</th>\n",
121+
" <th>2019 [YR2019]</th>\n",
122+
" <th>2020 [YR2020]</th>\n",
123+
" <th>2021 [YR2021]</th>\n",
124+
" <th>2022 [YR2022]</th>\n",
125+
" <th>2023 [YR2023]</th>\n",
126+
" </tr>\n",
127+
" </thead>\n",
128+
" <tbody>\n",
129+
" <tr>\n",
130+
" <th>0</th>\n",
131+
" <td>Access to electricity (% of population)</td>\n",
132+
" <td>EG.ELC.ACCS.ZS</td>\n",
133+
" <td>Afghanistan</td>\n",
134+
" <td>AFG</td>\n",
135+
" <td>..</td>\n",
136+
" <td>4.4</td>\n",
137+
" <td>89.5</td>\n",
138+
" <td>71.5</td>\n",
139+
" <td>97.7</td>\n",
140+
" <td>97.7</td>\n",
141+
" <td>93.4</td>\n",
142+
" <td>97.7</td>\n",
143+
" <td>97.7</td>\n",
144+
" <td>97.7</td>\n",
145+
" <td>85.3</td>\n",
146+
" <td>..</td>\n",
147+
" </tr>\n",
148+
" <tr>\n",
149+
" <th>1</th>\n",
150+
" <td>Access to electricity (% of population)</td>\n",
151+
" <td>EG.ELC.ACCS.ZS</td>\n",
152+
" <td>Albania</td>\n",
153+
" <td>ALB</td>\n",
154+
" <td>100</td>\n",
155+
" <td>99.4</td>\n",
156+
" <td>100</td>\n",
157+
" <td>100</td>\n",
158+
" <td>99.9</td>\n",
159+
" <td>99.9</td>\n",
160+
" <td>100</td>\n",
161+
" <td>100</td>\n",
162+
" <td>100</td>\n",
163+
" <td>100</td>\n",
164+
" <td>100</td>\n",
165+
" <td>..</td>\n",
166+
" </tr>\n",
167+
" <tr>\n",
168+
" <th>2</th>\n",
169+
" <td>Access to electricity (% of population)</td>\n",
170+
" <td>EG.ELC.ACCS.ZS</td>\n",
171+
" <td>Algeria</td>\n",
172+
" <td>DZA</td>\n",
173+
" <td>..</td>\n",
174+
" <td>98.6</td>\n",
175+
" <td>99.3</td>\n",
176+
" <td>99.4</td>\n",
177+
" <td>99.4</td>\n",
178+
" <td>99.5</td>\n",
179+
" <td>99.6</td>\n",
180+
" <td>99.5</td>\n",
181+
" <td>99.7</td>\n",
182+
" <td>99.8</td>\n",
183+
" <td>100</td>\n",
184+
" <td>..</td>\n",
185+
" </tr>\n",
186+
" <tr>\n",
187+
" <th>3</th>\n",
188+
" <td>Access to electricity (% of population)</td>\n",
189+
" <td>EG.ELC.ACCS.ZS</td>\n",
190+
" <td>American Samoa</td>\n",
191+
" <td>ASM</td>\n",
192+
" <td>..</td>\n",
193+
" <td>..</td>\n",
194+
" <td>..</td>\n",
195+
" <td>..</td>\n",
196+
" <td>..</td>\n",
197+
" <td>..</td>\n",
198+
" <td>..</td>\n",
199+
" <td>..</td>\n",
200+
" <td>..</td>\n",
201+
" <td>..</td>\n",
202+
" <td>..</td>\n",
203+
" <td>..</td>\n",
204+
" </tr>\n",
205+
" <tr>\n",
206+
" <th>4</th>\n",
207+
" <td>Access to electricity (% of population)</td>\n",
208+
" <td>EG.ELC.ACCS.ZS</td>\n",
209+
" <td>Andorra</td>\n",
210+
" <td>AND</td>\n",
211+
" <td>100</td>\n",
212+
" <td>100</td>\n",
213+
" <td>100</td>\n",
214+
" <td>100</td>\n",
215+
" <td>100</td>\n",
216+
" <td>100</td>\n",
217+
" <td>100</td>\n",
218+
" <td>100</td>\n",
219+
" <td>100</td>\n",
220+
" <td>100</td>\n",
221+
" <td>100</td>\n",
222+
" <td>..</td>\n",
223+
" </tr>\n",
224+
" </tbody>\n",
225+
"</table>\n",
226+
"</div>"
227+
],
228+
"text/plain": [
229+
" Series Name Series Code Country Name \\\n",
230+
"0 Access to electricity (% of population) EG.ELC.ACCS.ZS Afghanistan \n",
231+
"1 Access to electricity (% of population) EG.ELC.ACCS.ZS Albania \n",
232+
"2 Access to electricity (% of population) EG.ELC.ACCS.ZS Algeria \n",
233+
"3 Access to electricity (% of population) EG.ELC.ACCS.ZS American Samoa \n",
234+
"4 Access to electricity (% of population) EG.ELC.ACCS.ZS Andorra \n",
235+
"\n",
236+
" Country Code 1990 [YR1990] 2000 [YR2000] 2014 [YR2014] 2015 [YR2015] \\\n",
237+
"0 AFG .. 4.4 89.5 71.5 \n",
238+
"1 ALB 100 99.4 100 100 \n",
239+
"2 DZA .. 98.6 99.3 99.4 \n",
240+
"3 ASM .. .. .. .. \n",
241+
"4 AND 100 100 100 100 \n",
242+
"\n",
243+
" 2016 [YR2016] 2017 [YR2017] 2018 [YR2018] 2019 [YR2019] 2020 [YR2020] \\\n",
244+
"0 97.7 97.7 93.4 97.7 97.7 \n",
245+
"1 99.9 99.9 100 100 100 \n",
246+
"2 99.4 99.5 99.6 99.5 99.7 \n",
247+
"3 .. .. .. .. .. \n",
248+
"4 100 100 100 100 100 \n",
249+
"\n",
250+
" 2021 [YR2021] 2022 [YR2022] 2023 [YR2023] \n",
251+
"0 97.7 85.3 .. \n",
252+
"1 100 100 .. \n",
253+
"2 99.8 100 .. \n",
254+
"3 .. .. .. \n",
255+
"4 100 100 .. "
256+
]
257+
},
258+
"execution_count": 3,
259+
"metadata": {},
260+
"output_type": "execute_result"
261+
}
262+
],
263+
"source": [
264+
"df.head()"
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": 4,
270+
"metadata": {},
17271
"outputs": [
18272
{
19273
"name": "stdout",
@@ -31,6 +285,34 @@
31285
"print(con.execute(\"SHOW TABLES\").fetchall())"
32286
]
33287
},
288+
{
289+
"cell_type": "code",
290+
"execution_count": 5,
291+
"metadata": {},
292+
"outputs": [
293+
{
294+
"name": "stdout",
295+
"output_type": "stream",
296+
"text": [
297+
"[('electricity',)]\n"
298+
]
299+
}
300+
],
301+
"source": [
302+
"# create a connection to the world_bank.db\n",
303+
"con2 = duckdb.connect('world_bank.db')\n",
304+
"\n",
305+
"# show all tables in the database\n",
306+
"print(con2.execute(\"SHOW TABLES\").fetchall())"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": null,
312+
"metadata": {},
313+
"outputs": [],
314+
"source": []
315+
},
34316
{
35317
"cell_type": "code",
36318
"execution_count": 3,
@@ -265,7 +547,7 @@
265547
},
266548
{
267549
"cell_type": "code",
268-
"execution_count": 5,
550+
"execution_count": 6,
269551
"metadata": {},
270552
"outputs": [],
271553
"source": [

lab_07/world_bank.db

524 KB
Binary file not shown.

0 commit comments

Comments
 (0)