├── .gitignore
├── LICENSE
├── README.md
├── data
└── united_2022.csv
├── exercises
├── 01_basics.ipynb
└── 02_pandas.ipynb
├── lectures
├── 01_introduction.html
├── 01_introduction.ipynb
├── 02_basics.html
├── 02_basics.ipynb
├── 03_pandas.html
├── 03_pandas.ipynb
├── 04_data.html
├── 04_data.ipynb
└── imgs
│ ├── Stevens1946_tab1.png
│ ├── Tufte2001_napoleon.png
│ ├── bts.png
│ ├── butterfly_meme.jpg
│ ├── europe_diagram.png
│ ├── guido.gif
│ ├── jupyter_notebook_1.png
│ ├── jupyter_notebook_2.png
│ ├── kaggle_ide.png
│ ├── michael_scott.jpg
│ ├── punchcard.jpg
│ ├── python_logo.png
│ ├── python_monty.png
│ ├── python_snake.jpg
│ ├── stats_languages.jpg
│ ├── tidy_data.png
│ ├── tiobe_index.png
│ ├── united_breaks.png
│ ├── venn_diagram_sets.png
│ ├── what_a_week.jpg
│ ├── xkcd_163.png
│ └── xkcd_353.png
├── requirements.in
├── requirements.txt
└── syllabus
└── Introduction_to_Python_DS3.pdf
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | pip-wheel-metadata/
24 | share/python-wheels/
25 | *.egg-info/
26 | .installed.cfg
27 | *.egg
28 | MANIFEST
29 |
30 | # PyInstaller
31 | # Usually these files are written by a python script from a template
32 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
33 | *.manifest
34 | *.spec
35 |
36 | # Installer logs
37 | pip-log.txt
38 | pip-delete-this-directory.txt
39 |
40 | # Unit test / coverage reports
41 | htmlcov/
42 | .tox/
43 | .nox/
44 | .coverage
45 | .coverage.*
46 | .cache
47 | nosetests.xml
48 | coverage.xml
49 | *.cover
50 | *.py,cover
51 | .hypothesis/
52 | .pytest_cache/
53 |
54 | # Translations
55 | *.mo
56 | *.pot
57 |
58 | # Django stuff:
59 | *.log
60 | local_settings.py
61 | db.sqlite3
62 | db.sqlite3-journal
63 |
64 | # Flask stuff:
65 | instance/
66 | .webassets-cache
67 |
68 | # Scrapy stuff:
69 | .scrapy
70 |
71 | # Sphinx documentation
72 | docs/_build/
73 |
74 | # PyBuilder
75 | target/
76 |
77 | # Jupyter Notebook
78 | .ipynb_checkpoints
79 |
80 | # IPython
81 | profile_default/
82 | ipython_config.py
83 |
84 | # pyenv
85 | .python-version
86 |
87 | # pipenv
88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies
90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not
91 | # install all needed dependencies.
92 | #Pipfile.lock
93 |
94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow
95 | __pypackages__/
96 |
97 | # Celery stuff
98 | celerybeat-schedule
99 | celerybeat.pid
100 |
101 | # SageMath parsed files
102 | *.sage.py
103 |
104 | # Environments
105 | .env
106 | .venv
107 | env/
108 | venv/
109 | ENV/
110 | env.bak/
111 | venv.bak/
112 |
113 | # Spyder project settings
114 | .spyderproject
115 | .spyproject
116 |
117 | # Rope project settings
118 | .ropeproject
119 |
120 | # mkdocs documentation
121 | /site
122 |
123 | # mypy
124 | .mypy_cache/
125 | .dmypy.json
126 | dmypy.json
127 |
128 | # Pyre type checker
129 | .pyre/
130 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Attribution-NonCommercial-ShareAlike 4.0 International
2 |
3 | =======================================================================
4 |
5 | Creative Commons Corporation ("Creative Commons") is not a law firm and
6 | does not provide legal services or legal advice. Distribution of
7 | Creative Commons public licenses does not create a lawyer-client or
8 | other relationship. Creative Commons makes its licenses and related
9 | information available on an "as-is" basis. Creative Commons gives no
10 | warranties regarding its licenses, any material licensed under their
11 | terms and conditions, or any related information. Creative Commons
12 | disclaims all liability for damages resulting from their use to the
13 | fullest extent possible.
14 |
15 | Using Creative Commons Public Licenses
16 |
17 | Creative Commons public licenses provide a standard set of terms and
18 | conditions that creators and other rights holders may use to share
19 | original works of authorship and other material subject to copyright
20 | and certain other rights specified in the public license below. The
21 | following considerations are for informational purposes only, are not
22 | exhaustive, and do not form part of our licenses.
23 |
24 | Considerations for licensors: Our public licenses are
25 | intended for use by those authorized to give the public
26 | permission to use material in ways otherwise restricted by
27 | copyright and certain other rights. Our licenses are
28 | irrevocable. Licensors should read and understand the terms
29 | and conditions of the license they choose before applying it.
30 | Licensors should also secure all rights necessary before
31 | applying our licenses so that the public can reuse the
32 | material as expected. Licensors should clearly mark any
33 | material not subject to the license. This includes other CC-
34 | licensed material, or material used under an exception or
35 | limitation to copyright. More considerations for licensors:
36 | wiki.creativecommons.org/Considerations_for_licensors
37 |
38 | Considerations for the public: By using one of our public
39 | licenses, a licensor grants the public permission to use the
40 | licensed material under specified terms and conditions. If
41 | the licensor's permission is not necessary for any reason--for
42 | example, because of any applicable exception or limitation to
43 | copyright--then that use is not regulated by the license. Our
44 | licenses grant only permissions under copyright and certain
45 | other rights that a licensor has authority to grant. Use of
46 | the licensed material may still be restricted for other
47 | reasons, including because others have copyright or other
48 | rights in the material. A licensor may make special requests,
49 | such as asking that all changes be marked or described.
50 | Although not required by our licenses, you are encouraged to
51 | respect those requests where reasonable. More considerations
52 | for the public:
53 | wiki.creativecommons.org/Considerations_for_licensees
54 |
55 | =======================================================================
56 |
57 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
58 | Public License
59 |
60 | By exercising the Licensed Rights (defined below), You accept and agree
61 | to be bound by the terms and conditions of this Creative Commons
62 | Attribution-NonCommercial-ShareAlike 4.0 International Public License
63 | ("Public License"). To the extent this Public License may be
64 | interpreted as a contract, You are granted the Licensed Rights in
65 | consideration of Your acceptance of these terms and conditions, and the
66 | Licensor grants You such rights in consideration of benefits the
67 | Licensor receives from making the Licensed Material available under
68 | these terms and conditions.
69 |
70 |
71 | Section 1 -- Definitions.
72 |
73 | a. Adapted Material means material subject to Copyright and Similar
74 | Rights that is derived from or based upon the Licensed Material
75 | and in which the Licensed Material is translated, altered,
76 | arranged, transformed, or otherwise modified in a manner requiring
77 | permission under the Copyright and Similar Rights held by the
78 | Licensor. For purposes of this Public License, where the Licensed
79 | Material is a musical work, performance, or sound recording,
80 | Adapted Material is always produced where the Licensed Material is
81 | synched in timed relation with a moving image.
82 |
83 | b. Adapter's License means the license You apply to Your Copyright
84 | and Similar Rights in Your contributions to Adapted Material in
85 | accordance with the terms and conditions of this Public License.
86 |
87 | c. BY-NC-SA Compatible License means a license listed at
88 | creativecommons.org/compatiblelicenses, approved by Creative
89 | Commons as essentially the equivalent of this Public License.
90 |
91 | d. Copyright and Similar Rights means copyright and/or similar rights
92 | closely related to copyright including, without limitation,
93 | performance, broadcast, sound recording, and Sui Generis Database
94 | Rights, without regard to how the rights are labeled or
95 | categorized. For purposes of this Public License, the rights
96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar
97 | Rights.
98 |
99 | e. Effective Technological Measures means those measures that, in the
100 | absence of proper authority, may not be circumvented under laws
101 | fulfilling obligations under Article 11 of the WIPO Copyright
102 | Treaty adopted on December 20, 1996, and/or similar international
103 | agreements.
104 |
105 | f. Exceptions and Limitations means fair use, fair dealing, and/or
106 | any other exception or limitation to Copyright and Similar Rights
107 | that applies to Your use of the Licensed Material.
108 |
109 | g. License Elements means the license attributes listed in the name
110 | of a Creative Commons Public License. The License Elements of this
111 | Public License are Attribution, NonCommercial, and ShareAlike.
112 |
113 | h. Licensed Material means the artistic or literary work, database,
114 | or other material to which the Licensor applied this Public
115 | License.
116 |
117 | i. Licensed Rights means the rights granted to You subject to the
118 | terms and conditions of this Public License, which are limited to
119 | all Copyright and Similar Rights that apply to Your use of the
120 | Licensed Material and that the Licensor has authority to license.
121 |
122 | j. Licensor means the individual(s) or entity(ies) granting rights
123 | under this Public License.
124 |
125 | k. NonCommercial means not primarily intended for or directed towards
126 | commercial advantage or monetary compensation. For purposes of
127 | this Public License, the exchange of the Licensed Material for
128 | other material subject to Copyright and Similar Rights by digital
129 | file-sharing or similar means is NonCommercial provided there is
130 | no payment of monetary compensation in connection with the
131 | exchange.
132 |
133 | l. Share means to provide material to the public by any means or
134 | process that requires permission under the Licensed Rights, such
135 | as reproduction, public display, public performance, distribution,
136 | dissemination, communication, or importation, and to make material
137 | available to the public including in ways that members of the
138 | public may access the material from a place and at a time
139 | individually chosen by them.
140 |
141 | m. Sui Generis Database Rights means rights other than copyright
142 | resulting from Directive 96/9/EC of the European Parliament and of
143 | the Council of 11 March 1996 on the legal protection of databases,
144 | as amended and/or succeeded, as well as other essentially
145 | equivalent rights anywhere in the world.
146 |
147 | n. You means the individual or entity exercising the Licensed Rights
148 | under this Public License. Your has a corresponding meaning.
149 |
150 |
151 | Section 2 -- Scope.
152 |
153 | a. License grant.
154 |
155 | 1. Subject to the terms and conditions of this Public License,
156 | the Licensor hereby grants You a worldwide, royalty-free,
157 | non-sublicensable, non-exclusive, irrevocable license to
158 | exercise the Licensed Rights in the Licensed Material to:
159 |
160 | a. reproduce and Share the Licensed Material, in whole or
161 | in part, for NonCommercial purposes only; and
162 |
163 | b. produce, reproduce, and Share Adapted Material for
164 | NonCommercial purposes only.
165 |
166 | 2. Exceptions and Limitations. For the avoidance of doubt, where
167 | Exceptions and Limitations apply to Your use, this Public
168 | License does not apply, and You do not need to comply with
169 | its terms and conditions.
170 |
171 | 3. Term. The term of this Public License is specified in Section
172 | 6(a).
173 |
174 | 4. Media and formats; technical modifications allowed. The
175 | Licensor authorizes You to exercise the Licensed Rights in
176 | all media and formats whether now known or hereafter created,
177 | and to make technical modifications necessary to do so. The
178 | Licensor waives and/or agrees not to assert any right or
179 | authority to forbid You from making technical modifications
180 | necessary to exercise the Licensed Rights, including
181 | technical modifications necessary to circumvent Effective
182 | Technological Measures. For purposes of this Public License,
183 | simply making modifications authorized by this Section 2(a)
184 | (4) never produces Adapted Material.
185 |
186 | 5. Downstream recipients.
187 |
188 | a. Offer from the Licensor -- Licensed Material. Every
189 | recipient of the Licensed Material automatically
190 | receives an offer from the Licensor to exercise the
191 | Licensed Rights under the terms and conditions of this
192 | Public License.
193 |
194 | b. Additional offer from the Licensor -- Adapted Material.
195 | Every recipient of Adapted Material from You
196 | automatically receives an offer from the Licensor to
197 | exercise the Licensed Rights in the Adapted Material
198 | under the conditions of the Adapter's License You apply.
199 |
200 | c. No downstream restrictions. You may not offer or impose
201 | any additional or different terms or conditions on, or
202 | apply any Effective Technological Measures to, the
203 | Licensed Material if doing so restricts exercise of the
204 | Licensed Rights by any recipient of the Licensed
205 | Material.
206 |
207 | 6. No endorsement. Nothing in this Public License constitutes or
208 | may be construed as permission to assert or imply that You
209 | are, or that Your use of the Licensed Material is, connected
210 | with, or sponsored, endorsed, or granted official status by,
211 | the Licensor or others designated to receive attribution as
212 | provided in Section 3(a)(1)(A)(i).
213 |
214 | b. Other rights.
215 |
216 | 1. Moral rights, such as the right of integrity, are not
217 | licensed under this Public License, nor are publicity,
218 | privacy, and/or other similar personality rights; however, to
219 | the extent possible, the Licensor waives and/or agrees not to
220 | assert any such rights held by the Licensor to the limited
221 | extent necessary to allow You to exercise the Licensed
222 | Rights, but not otherwise.
223 |
224 | 2. Patent and trademark rights are not licensed under this
225 | Public License.
226 |
227 | 3. To the extent possible, the Licensor waives any right to
228 | collect royalties from You for the exercise of the Licensed
229 | Rights, whether directly or through a collecting society
230 | under any voluntary or waivable statutory or compulsory
231 | licensing scheme. In all other cases the Licensor expressly
232 | reserves any right to collect such royalties, including when
233 | the Licensed Material is used other than for NonCommercial
234 | purposes.
235 |
236 |
237 | Section 3 -- License Conditions.
238 |
239 | Your exercise of the Licensed Rights is expressly made subject to the
240 | following conditions.
241 |
242 | a. Attribution.
243 |
244 | 1. If You Share the Licensed Material (including in modified
245 | form), You must:
246 |
247 | a. retain the following if it is supplied by the Licensor
248 | with the Licensed Material:
249 |
250 | i. identification of the creator(s) of the Licensed
251 | Material and any others designated to receive
252 | attribution, in any reasonable manner requested by
253 | the Licensor (including by pseudonym if
254 | designated);
255 |
256 | ii. a copyright notice;
257 |
258 | iii. a notice that refers to this Public License;
259 |
260 | iv. a notice that refers to the disclaimer of
261 | warranties;
262 |
263 | v. a URI or hyperlink to the Licensed Material to the
264 | extent reasonably practicable;
265 |
266 | b. indicate if You modified the Licensed Material and
267 | retain an indication of any previous modifications; and
268 |
269 | c. indicate the Licensed Material is licensed under this
270 | Public License, and include the text of, or the URI or
271 | hyperlink to, this Public License.
272 |
273 | 2. You may satisfy the conditions in Section 3(a)(1) in any
274 | reasonable manner based on the medium, means, and context in
275 | which You Share the Licensed Material. For example, it may be
276 | reasonable to satisfy the conditions by providing a URI or
277 | hyperlink to a resource that includes the required
278 | information.
279 | 3. If requested by the Licensor, You must remove any of the
280 | information required by Section 3(a)(1)(A) to the extent
281 | reasonably practicable.
282 |
283 | b. ShareAlike.
284 |
285 | In addition to the conditions in Section 3(a), if You Share
286 | Adapted Material You produce, the following conditions also apply.
287 |
288 | 1. The Adapter's License You apply must be a Creative Commons
289 | license with the same License Elements, this version or
290 | later, or a BY-NC-SA Compatible License.
291 |
292 | 2. You must include the text of, or the URI or hyperlink to, the
293 | Adapter's License You apply. You may satisfy this condition
294 | in any reasonable manner based on the medium, means, and
295 | context in which You Share Adapted Material.
296 |
297 | 3. You may not offer or impose any additional or different terms
298 | or conditions on, or apply any Effective Technological
299 | Measures to, Adapted Material that restrict exercise of the
300 | rights granted under the Adapter's License You apply.
301 |
302 |
303 | Section 4 -- Sui Generis Database Rights.
304 |
305 | Where the Licensed Rights include Sui Generis Database Rights that
306 | apply to Your use of the Licensed Material:
307 |
308 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right
309 | to extract, reuse, reproduce, and Share all or a substantial
310 | portion of the contents of the database for NonCommercial purposes
311 | only;
312 |
313 | b. if You include all or a substantial portion of the database
314 | contents in a database in which You have Sui Generis Database
315 | Rights, then the database in which You have Sui Generis Database
316 | Rights (but not its individual contents) is Adapted Material,
317 | including for purposes of Section 3(b); and
318 |
319 | c. You must comply with the conditions in Section 3(a) if You Share
320 | all or a substantial portion of the contents of the database.
321 |
322 | For the avoidance of doubt, this Section 4 supplements and does not
323 | replace Your obligations under this Public License where the Licensed
324 | Rights include other Copyright and Similar Rights.
325 |
326 |
327 | Section 5 -- Disclaimer of Warranties and Limitation of Liability.
328 |
329 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
330 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
331 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
332 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
333 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
334 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
335 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
336 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
337 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
338 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
339 |
340 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
341 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
342 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
343 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
344 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
345 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
346 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
347 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
348 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
349 |
350 | c. The disclaimer of warranties and limitation of liability provided
351 | above shall be interpreted in a manner that, to the extent
352 | possible, most closely approximates an absolute disclaimer and
353 | waiver of all liability.
354 |
355 |
356 | Section 6 -- Term and Termination.
357 |
358 | a. This Public License applies for the term of the Copyright and
359 | Similar Rights licensed here. However, if You fail to comply with
360 | this Public License, then Your rights under this Public License
361 | terminate automatically.
362 |
363 | b. Where Your right to use the Licensed Material has terminated under
364 | Section 6(a), it reinstates:
365 |
366 | 1. automatically as of the date the violation is cured, provided
367 | it is cured within 30 days of Your discovery of the
368 | violation; or
369 |
370 | 2. upon express reinstatement by the Licensor.
371 |
372 | For the avoidance of doubt, this Section 6(b) does not affect any
373 | right the Licensor may have to seek remedies for Your violations
374 | of this Public License.
375 |
376 | c. For the avoidance of doubt, the Licensor may also offer the
377 | Licensed Material under separate terms or conditions or stop
378 | distributing the Licensed Material at any time; however, doing so
379 | will not terminate this Public License.
380 |
381 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
382 | License.
383 |
384 |
385 | Section 7 -- Other Terms and Conditions.
386 |
387 | a. The Licensor shall not be bound by any additional or different
388 | terms or conditions communicated by You unless expressly agreed.
389 |
390 | b. Any arrangements, understandings, or agreements regarding the
391 | Licensed Material not stated herein are separate from and
392 | independent of the terms and conditions of this Public License.
393 |
394 |
395 | Section 8 -- Interpretation.
396 |
397 | a. For the avoidance of doubt, this Public License does not, and
398 | shall not be interpreted to, reduce, limit, restrict, or impose
399 | conditions on any use of the Licensed Material that could lawfully
400 | be made without permission under this Public License.
401 |
402 | b. To the extent possible, if any provision of this Public License is
403 | deemed unenforceable, it shall be automatically reformed to the
404 | minimum extent necessary to make it enforceable. If the provision
405 | cannot be reformed, it shall be severed from this Public License
406 | without affecting the enforceability of the remaining terms and
407 | conditions.
408 |
409 | c. No term or condition of this Public License will be waived and no
410 | failure to comply consented to unless expressly agreed to by the
411 | Licensor.
412 |
413 | d. Nothing in this Public License constitutes or may be interpreted
414 | as a limitation upon, or waiver of, any privileges and immunities
415 | that apply to the Licensor or You, including from the legal
416 | processes of any jurisdiction or authority.
417 |
418 | =======================================================================
419 |
420 | Creative Commons is not a party to its public
421 | licenses. Notwithstanding, Creative Commons may elect to apply one of
422 | its public licenses to material it publishes and in those instances
423 | will be considered the “Licensor.” The text of the Creative Commons
424 | public licenses is dedicated to the public domain under the CC0 Public
425 | Domain Dedication. Except for the limited purpose of indicating that
426 | material is shared under a Creative Commons public license or as
427 | otherwise permitted by the Creative Commons policies published at
428 | creativecommons.org/policies, Creative Commons does not authorize the
429 | use of the trademark "Creative Commons" or any other trademark or logo
430 | of Creative Commons without its prior written consent including,
431 | without limitation, in connection with any unauthorized modifications
432 | to any of its public licenses or any other arrangements,
433 | understandings, or agreements concerning use of licensed material. For
434 | the avoidance of doubt, this paragraph does not form part of the
435 | public licenses.
436 |
437 | Creative Commons may be contacted at creativecommons.org.
438 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DS3_Introduction_Python
2 |
3 | Materials for 1-day workshop DS3 [Introduction to Python](https://ds3.ai/2022/python.html) workshop
4 |
5 | ## Structure
6 |
7 | - `./data` - Data files used in the workshop
8 | - `./exercises` - Jupyter Notebooks with class exercises
9 | - `./lectures` - Lecture materials (as Jupyter Notebooks and compiled PDF/HTML files)
10 | - `./syllabus` - Copy of workshop syllabus
11 |
12 | ## Schedule
13 |
14 | | Date | Time (CEST) | Topic |
15 | |:--------|:--------------|:-------------------------------------------------|
16 | | 27 July | 15:00-16:45 | Introduction to Python objects and data types |
17 | | | 16:45-17:00 | Exercise I |
18 | | | 17:00-17:15 | Break |
19 | | | 17:15-18:00 | Introduction to Pandas |
20 | | | 18:00-19:00 | Exploratory data analysis and data visualization |
21 | | | 19:00-19:15 | Exercise II |
22 |
23 | ## Jupyter Notebook Installation
24 |
25 | - For this workshop I recommend using one of the 2 online platforms for working with Jupyter Noteboks:
26 | - [Google Colab](https://colab.research.google.com/notebooks/intro.ipynb), a cloud platform for hosting Jupyter Notebooks. You need to have a Google account, but it does not require any local installations.
27 | - [Kaggle Code](https://www.kaggle.com/code), a platform for sharing and exploring data-science-focussed Jupyter Notebooks. Although technically owned by Google, you can register just for Kaggle website.
28 | - If you would prefer to install Jupyter Notebook on your local machine, there are two main ways to do this: [pip](https://jupyter.readthedocs.io/en/latest/install/notebook-classic.html#alternative-for-experienced-python-users-installing-jupyter-with-pip) and [conda](https://jupyter.readthedocs.io/en/latest/install/notebook-classic.html#installing-jupyter-using-anaconda-and-conda). Unless you have prior experience with Python, I recommend installing [Anaconda](https://www.anaconda.com/products/individual) distribution, which contains all the packages required for this course.
29 |
30 | ## Additional Materials
31 |
32 | There are many great online resources and published books on programming in Python. Some of them also provide a good coverage of using Python for data analysis. Here are some pointers to start from:
33 |
34 | Books:
35 |
36 | - Guttag, John. 2021 *Introduction to Computation and Programming Using Python: With Application to Computational Modeling and Understanding Data*. 3rd ed. Cambridge, MA: The MIT Press
37 |
38 | - McKinney, Wes. 2017. *Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython*. 2nd ed. Sebastopol, CA: O'Reilly Media
39 |
40 | - Sweigart, Al. 2019. *Automate the Boring Stuff with Python*. 2nd ed. San Francisco, CA: No
41 | Starch Press
42 |
43 | Online:
44 |
45 | - [Python For You and Me](https://pymbook.readthedocs.io/en/latest/)
46 |
47 | - [Python Wikibook](https://en.wikibooks.org/wiki/Python_Programming)
48 |
49 | - [Python 3 Documentation](https://docs.python.org/3/) (intermediate and advanced)
50 |
51 | ---
52 |
53 | ## Recording
54 |
55 | You can watch the recording of the 2022 workshop at the link below:
56 |
57 | [](https://www.youtube.com/watch?v=YmcA4ODpiqA)
58 |
59 | ---
60 |
61 | ## License
62 |
63 | This work is licensed under a
64 | [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License][cc-by-nc-sa].
65 |
66 | [![CC BY-NC-SA 4.0][cc-by-nc-sa-image]][cc-by-nc-sa]
67 |
68 | [cc-by-nc-sa]: http://creativecommons.org/licenses/by-nc-sa/4.0/
69 | [cc-by-nc-sa-image]: https://licensebuttons.net/l/by-nc-sa/4.0/88x31.png
70 | [cc-by-nc-sa-shield]: https://img.shields.io/badge/License-CC%20BY--NC--SA%204.0-lightgrey.svg
--------------------------------------------------------------------------------
/exercises/01_basics.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "0c732118",
6 | "metadata": {},
7 | "source": [
8 | "## Exercise 1 - Python basics"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "d4c5b464",
14 | "metadata": {},
15 | "source": [
16 | "### Task 1: Slicing (Indexing)\n",
17 | "\n",
18 | "Using only slicing operations create words 'dish', 'wash', 'he' and 'saw' out of the word 'dishwasher'. Finally, reverse the order of letters in the word (spell it backwards). Print the results."
19 | ]
20 | },
21 | {
22 | "cell_type": "code",
23 | "execution_count": 1,
24 | "id": "09113bcd",
25 | "metadata": {},
26 | "outputs": [],
27 | "source": [
28 | "dishwasher = 'dishwasher'"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 2,
34 | "id": "869c6a46",
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "## Add you code here"
39 | ]
40 | },
41 | {
42 | "cell_type": "markdown",
43 | "id": "8eff1c1d",
44 | "metadata": {},
45 | "source": [
46 | "### Task 2: Sets\n",
47 | "\n",
48 | "Create a set called `visegrad`, which includes all the countries from the Visegrád Group\n",
49 | "\n",
50 | "Tip: If any flags are unfamiliar, check the interactive version of the diagram [here](https://upload.wikimedia.org/wikipedia/commons/6/6a/Supranational_European_Bodies.svg)"
51 | ]
52 | },
53 | {
54 | "cell_type": "markdown",
55 | "id": "3bef15d4",
56 | "metadata": {},
57 | "source": [
58 | "\n",
59 | "\n",
60 | "Source: [Wikipedia](https://en.wikipedia.org/w/index.php?title=File:Supranational_European_Bodies-en.svg)"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": 3,
66 | "id": "96b78d0b",
67 | "metadata": {},
68 | "outputs": [],
69 | "source": [
70 | "## Add you code here"
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "id": "e144dc01",
76 | "metadata": {},
77 | "source": [
78 | "### Task 3: Lists and Sets\n",
79 | "\n",
80 | "Create lists `baltic` and `benelux` with respective countries. Convert them into sets. Create a union of these states and countries from the Visegrád Group"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 4,
86 | "id": "03647a54",
87 | "metadata": {},
88 | "outputs": [],
89 | "source": [
90 | "## Add you code here"
91 | ]
92 | },
93 | {
94 | "cell_type": "markdown",
95 | "id": "eb22b0f5",
96 | "metadata": {},
97 | "source": [
98 | "### (Extra) Task 4: Dictionaries\n",
99 | "\n",
100 | "Create a dictionary, where the country name is the key and the value is the capital (or currency) of that country\n",
101 | "\n",
102 | "Tip: You can start by modifying `visegrad` set, use interactive version of the diagram for addiional information."
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 5,
108 | "id": "d96e65db",
109 | "metadata": {},
110 | "outputs": [],
111 | "source": [
112 | "## Add you code here"
113 | ]
114 | }
115 | ],
116 | "metadata": {
117 | "kernelspec": {
118 | "display_name": "Python 3",
119 | "language": "python",
120 | "name": "python3"
121 | },
122 | "language_info": {
123 | "codemirror_mode": {
124 | "name": "ipython",
125 | "version": 3
126 | },
127 | "file_extension": ".py",
128 | "mimetype": "text/x-python",
129 | "name": "python",
130 | "nbconvert_exporter": "python",
131 | "pygments_lexer": "ipython3",
132 | "version": "3.8.10"
133 | }
134 | },
135 | "nbformat": 4,
136 | "nbformat_minor": 5
137 | }
138 |
--------------------------------------------------------------------------------
/exercises/02_pandas.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "59f41627",
6 | "metadata": {},
7 | "source": [
8 | "## Exercise 2 - Pandas"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "5b49ef09",
14 | "metadata": {},
15 | "source": [
16 | "Let's try subsetting and filtering on the United flights dataset"
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": 1,
22 | "id": "09b56bc8",
23 | "metadata": {},
24 | "outputs": [],
25 | "source": [
26 | "import pandas as pd"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "execution_count": 2,
32 | "id": "0ae79f4a",
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "## Read in the data\n",
37 | "united_2022 = pd.read_csv('https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/main/data/united_2022.csv')"
38 | ]
39 | },
40 | {
41 | "cell_type": "code",
42 | "execution_count": 3,
43 | "id": "f23ea224",
44 | "metadata": {},
45 | "outputs": [
46 | {
47 | "data": {
48 | "text/html": [
49 | "
\n",
50 | "\n",
63 | "
\n",
64 | " \n",
65 | " \n",
66 | " | \n",
67 | " Carrier Code | \n",
68 | " Date (MM/DD/YYYY) | \n",
69 | " Flight Number | \n",
70 | " Tail Number | \n",
71 | " Origin Airport | \n",
72 | " Destination Airport | \n",
73 | " Scheduled departure time | \n",
74 | " Actual departure time | \n",
75 | " Scheduled elapsed time (Minutes) | \n",
76 | " Actual elapsed time (Minutes) | \n",
77 | " Departure delay (Minutes) | \n",
78 | " Wheels-off time | \n",
79 | " Taxi-Out time (Minutes) | \n",
80 | " Delay Carrier (Minutes) | \n",
81 | " Delay Weather (Minutes) | \n",
82 | " Delay National Aviation System (Minutes) | \n",
83 | " Delay Security (Minutes) | \n",
84 | " Delay Late Aircraft Arrival (Minutes) | \n",
85 | "
\n",
86 | " \n",
87 | " \n",
88 | " \n",
89 | " 0 | \n",
90 | " UA | \n",
91 | " 01/01/2022 | \n",
92 | " 225 | \n",
93 | " N488UA | \n",
94 | " ATL | \n",
95 | " DEN | \n",
96 | " 16:15:00 | \n",
97 | " 17:23:00 | \n",
98 | " 211 | \n",
99 | " 240 | \n",
100 | " 68 | \n",
101 | " 17:35:00 | \n",
102 | " 12 | \n",
103 | " 0 | \n",
104 | " 0 | \n",
105 | " 29 | \n",
106 | " 0 | \n",
107 | " 68 | \n",
108 | "
\n",
109 | " \n",
110 | " 1 | \n",
111 | " UA | \n",
112 | " 01/01/2022 | \n",
113 | " 282 | \n",
114 | " N447UA | \n",
115 | " ATL | \n",
116 | " IAH | \n",
117 | " 19:00:00 | \n",
118 | " 19:02:00 | \n",
119 | " 138 | \n",
120 | " 126 | \n",
121 | " 2 | \n",
122 | " 19:15:00 | \n",
123 | " 13 | \n",
124 | " 0 | \n",
125 | " 0 | \n",
126 | " 0 | \n",
127 | " 0 | \n",
128 | " 0 | \n",
129 | "
\n",
130 | " \n",
131 | " 2 | \n",
132 | " UA | \n",
133 | " 01/01/2022 | \n",
134 | " 340 | \n",
135 | " N809UA | \n",
136 | " ATL | \n",
137 | " DEN | \n",
138 | " 08:20:00 | \n",
139 | " 08:17:00 | \n",
140 | " 211 | \n",
141 | " 283 | \n",
142 | " -3 | \n",
143 | " 08:33:00 | \n",
144 | " 16 | \n",
145 | " 0 | \n",
146 | " 0 | \n",
147 | " 69 | \n",
148 | " 0 | \n",
149 | " 0 | \n",
150 | "
\n",
151 | " \n",
152 | " 3 | \n",
153 | " UA | \n",
154 | " 01/02/2022 | \n",
155 | " 225 | \n",
156 | " N463UA | \n",
157 | " ATL | \n",
158 | " DEN | \n",
159 | " 16:15:00 | \n",
160 | " 16:36:00 | \n",
161 | " 211 | \n",
162 | " 193 | \n",
163 | " 21 | \n",
164 | " 16:48:00 | \n",
165 | " 12 | \n",
166 | " 0 | \n",
167 | " 0 | \n",
168 | " 0 | \n",
169 | " 0 | \n",
170 | " 0 | \n",
171 | "
\n",
172 | " \n",
173 | " 4 | \n",
174 | " UA | \n",
175 | " 01/02/2022 | \n",
176 | " 282 | \n",
177 | " N63899 | \n",
178 | " ATL | \n",
179 | " IAH | \n",
180 | " 19:00:00 | \n",
181 | " 18:54:00 | \n",
182 | " 138 | \n",
183 | " 129 | \n",
184 | " -6 | \n",
185 | " 19:07:00 | \n",
186 | " 13 | \n",
187 | " 0 | \n",
188 | " 0 | \n",
189 | " 0 | \n",
190 | " 0 | \n",
191 | " 0 | \n",
192 | "
\n",
193 | " \n",
194 | "
\n",
195 | "
"
196 | ],
197 | "text/plain": [
198 | " Carrier Code Date (MM/DD/YYYY) Flight Number Tail Number Origin Airport \\\n",
199 | "0 UA 01/01/2022 225 N488UA ATL \n",
200 | "1 UA 01/01/2022 282 N447UA ATL \n",
201 | "2 UA 01/01/2022 340 N809UA ATL \n",
202 | "3 UA 01/02/2022 225 N463UA ATL \n",
203 | "4 UA 01/02/2022 282 N63899 ATL \n",
204 | "\n",
205 | " Destination Airport Scheduled departure time Actual departure time \\\n",
206 | "0 DEN 16:15:00 17:23:00 \n",
207 | "1 IAH 19:00:00 19:02:00 \n",
208 | "2 DEN 08:20:00 08:17:00 \n",
209 | "3 DEN 16:15:00 16:36:00 \n",
210 | "4 IAH 19:00:00 18:54:00 \n",
211 | "\n",
212 | " Scheduled elapsed time (Minutes) Actual elapsed time (Minutes) \\\n",
213 | "0 211 240 \n",
214 | "1 138 126 \n",
215 | "2 211 283 \n",
216 | "3 211 193 \n",
217 | "4 138 129 \n",
218 | "\n",
219 | " Departure delay (Minutes) Wheels-off time Taxi-Out time (Minutes) \\\n",
220 | "0 68 17:35:00 12 \n",
221 | "1 2 19:15:00 13 \n",
222 | "2 -3 08:33:00 16 \n",
223 | "3 21 16:48:00 12 \n",
224 | "4 -6 19:07:00 13 \n",
225 | "\n",
226 | " Delay Carrier (Minutes) Delay Weather (Minutes) \\\n",
227 | "0 0 0 \n",
228 | "1 0 0 \n",
229 | "2 0 0 \n",
230 | "3 0 0 \n",
231 | "4 0 0 \n",
232 | "\n",
233 | " Delay National Aviation System (Minutes) Delay Security (Minutes) \\\n",
234 | "0 29 0 \n",
235 | "1 0 0 \n",
236 | "2 69 0 \n",
237 | "3 0 0 \n",
238 | "4 0 0 \n",
239 | "\n",
240 | " Delay Late Aircraft Arrival (Minutes) \n",
241 | "0 68 \n",
242 | "1 0 \n",
243 | "2 0 \n",
244 | "3 0 \n",
245 | "4 0 "
246 | ]
247 | },
248 | "execution_count": 3,
249 | "metadata": {},
250 | "output_type": "execute_result"
251 | }
252 | ],
253 | "source": [
254 | "## Show top 5 rows\n",
255 | "united_2022.head()"
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": 4,
261 | "id": "e45d046c",
262 | "metadata": {},
263 | "outputs": [
264 | {
265 | "data": {
266 | "text/html": [
267 | "\n",
268 | "\n",
281 | "
\n",
282 | " \n",
283 | " \n",
284 | " | \n",
285 | " Carrier Code | \n",
286 | " Date (MM/DD/YYYY) | \n",
287 | " Flight Number | \n",
288 | " Tail Number | \n",
289 | " Origin Airport | \n",
290 | " Destination Airport | \n",
291 | " Scheduled departure time | \n",
292 | " Actual departure time | \n",
293 | " Scheduled elapsed time (Minutes) | \n",
294 | " Actual elapsed time (Minutes) | \n",
295 | " Departure delay (Minutes) | \n",
296 | " Wheels-off time | \n",
297 | " Taxi-Out time (Minutes) | \n",
298 | " Delay Carrier (Minutes) | \n",
299 | " Delay Weather (Minutes) | \n",
300 | " Delay National Aviation System (Minutes) | \n",
301 | " Delay Security (Minutes) | \n",
302 | " Delay Late Aircraft Arrival (Minutes) | \n",
303 | "
\n",
304 | " \n",
305 | " \n",
306 | " \n",
307 | " 154570 | \n",
308 | " UA | \n",
309 | " 05/31/2022 | \n",
310 | " 2652 | \n",
311 | " N47524 | \n",
312 | " SFO | \n",
313 | " BOS | \n",
314 | " 13:25:00 | \n",
315 | " 13:19:00 | \n",
316 | " 354 | \n",
317 | " 318 | \n",
318 | " -6 | \n",
319 | " 13:38:00 | \n",
320 | " 19 | \n",
321 | " 0 | \n",
322 | " 0 | \n",
323 | " 0 | \n",
324 | " 0 | \n",
325 | " 0 | \n",
326 | "
\n",
327 | " \n",
328 | " 154571 | \n",
329 | " UA | \n",
330 | " 05/31/2022 | \n",
331 | " 2655 | \n",
332 | " N15969 | \n",
333 | " SFO | \n",
334 | " EWR | \n",
335 | " 08:50:00 | \n",
336 | " 10:19:00 | \n",
337 | " 327 | \n",
338 | " 343 | \n",
339 | " 89 | \n",
340 | " 10:37:00 | \n",
341 | " 18 | \n",
342 | " 89 | \n",
343 | " 0 | \n",
344 | " 16 | \n",
345 | " 0 | \n",
346 | " 0 | \n",
347 | "
\n",
348 | " \n",
349 | " 154572 | \n",
350 | " UA | \n",
351 | " 05/31/2022 | \n",
352 | " 2657 | \n",
353 | " N77431 | \n",
354 | " SFO | \n",
355 | " PHX | \n",
356 | " 19:00:00 | \n",
357 | " 18:52:00 | \n",
358 | " 119 | \n",
359 | " 115 | \n",
360 | " -8 | \n",
361 | " 19:09:00 | \n",
362 | " 17 | \n",
363 | " 0 | \n",
364 | " 0 | \n",
365 | " 0 | \n",
366 | " 0 | \n",
367 | " 0 | \n",
368 | "
\n",
369 | " \n",
370 | " 154573 | \n",
371 | " UA | \n",
372 | " 05/31/2022 | \n",
373 | " 2669 | \n",
374 | " N76523 | \n",
375 | " SFO | \n",
376 | " SAN | \n",
377 | " 10:54:00 | \n",
378 | " 10:57:00 | \n",
379 | " 101 | \n",
380 | " 85 | \n",
381 | " 3 | \n",
382 | " 11:11:00 | \n",
383 | " 14 | \n",
384 | " 0 | \n",
385 | " 0 | \n",
386 | " 0 | \n",
387 | " 0 | \n",
388 | " 0 | \n",
389 | "
\n",
390 | " \n",
391 | " 154574 | \n",
392 | " UA | \n",
393 | " 05/31/2022 | \n",
394 | " 2670 | \n",
395 | " N37253 | \n",
396 | " SFO | \n",
397 | " TPA | \n",
398 | " 09:59:00 | \n",
399 | " 09:52:00 | \n",
400 | " 310 | \n",
401 | " 305 | \n",
402 | " -7 | \n",
403 | " 10:08:00 | \n",
404 | " 16 | \n",
405 | " 0 | \n",
406 | " 0 | \n",
407 | " 0 | \n",
408 | " 0 | \n",
409 | " 0 | \n",
410 | "
\n",
411 | " \n",
412 | "
\n",
413 | "
"
414 | ],
415 | "text/plain": [
416 | " Carrier Code Date (MM/DD/YYYY) Flight Number Tail Number \\\n",
417 | "154570 UA 05/31/2022 2652 N47524 \n",
418 | "154571 UA 05/31/2022 2655 N15969 \n",
419 | "154572 UA 05/31/2022 2657 N77431 \n",
420 | "154573 UA 05/31/2022 2669 N76523 \n",
421 | "154574 UA 05/31/2022 2670 N37253 \n",
422 | "\n",
423 | " Origin Airport Destination Airport Scheduled departure time \\\n",
424 | "154570 SFO BOS 13:25:00 \n",
425 | "154571 SFO EWR 08:50:00 \n",
426 | "154572 SFO PHX 19:00:00 \n",
427 | "154573 SFO SAN 10:54:00 \n",
428 | "154574 SFO TPA 09:59:00 \n",
429 | "\n",
430 | " Actual departure time Scheduled elapsed time (Minutes) \\\n",
431 | "154570 13:19:00 354 \n",
432 | "154571 10:19:00 327 \n",
433 | "154572 18:52:00 119 \n",
434 | "154573 10:57:00 101 \n",
435 | "154574 09:52:00 310 \n",
436 | "\n",
437 | " Actual elapsed time (Minutes) Departure delay (Minutes) \\\n",
438 | "154570 318 -6 \n",
439 | "154571 343 89 \n",
440 | "154572 115 -8 \n",
441 | "154573 85 3 \n",
442 | "154574 305 -7 \n",
443 | "\n",
444 | " Wheels-off time Taxi-Out time (Minutes) Delay Carrier (Minutes) \\\n",
445 | "154570 13:38:00 19 0 \n",
446 | "154571 10:37:00 18 89 \n",
447 | "154572 19:09:00 17 0 \n",
448 | "154573 11:11:00 14 0 \n",
449 | "154574 10:08:00 16 0 \n",
450 | "\n",
451 | " Delay Weather (Minutes) Delay National Aviation System (Minutes) \\\n",
452 | "154570 0 0 \n",
453 | "154571 0 16 \n",
454 | "154572 0 0 \n",
455 | "154573 0 0 \n",
456 | "154574 0 0 \n",
457 | "\n",
458 | " Delay Security (Minutes) Delay Late Aircraft Arrival (Minutes) \n",
459 | "154570 0 0 \n",
460 | "154571 0 0 \n",
461 | "154572 0 0 \n",
462 | "154573 0 0 \n",
463 | "154574 0 0 "
464 | ]
465 | },
466 | "execution_count": 4,
467 | "metadata": {},
468 | "output_type": "execute_result"
469 | }
470 | ],
471 | "source": [
472 | "## Show bottom 5 rows\n",
473 | "united_2022.tail()"
474 | ]
475 | },
476 | {
477 | "cell_type": "markdown",
478 | "id": "8c4145bd",
479 | "metadata": {},
480 | "source": [
481 | "### Task 1: Subset rows 100 to 110 from the dataset"
482 | ]
483 | },
484 | {
485 | "cell_type": "code",
486 | "execution_count": 5,
487 | "id": "161de55d",
488 | "metadata": {},
489 | "outputs": [],
490 | "source": [
491 | "## Add you code here"
492 | ]
493 | },
494 | {
495 | "cell_type": "markdown",
496 | "id": "59cee502",
497 | "metadata": {},
498 | "source": [
499 | "### Task 2: Subset columns 1 to 5"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": 6,
505 | "id": "5ec05a07",
506 | "metadata": {},
507 | "outputs": [],
508 | "source": [
509 | "## Add you code here"
510 | ]
511 | },
512 | {
513 | "cell_type": "markdown",
514 | "id": "8ae12a53",
515 | "metadata": {},
516 | "source": [
517 | "### Task 3: Subset columns that list origin and destination airports\n",
518 | "\n",
519 | "Tip: You can use string method `contains()` to do that programatically. Check out an example from lecture where we used `startswith()` method."
520 | ]
521 | },
522 | {
523 | "cell_type": "code",
524 | "execution_count": 7,
525 | "id": "d0200ccb",
526 | "metadata": {},
527 | "outputs": [],
528 | "source": [
529 | "## Add you code here"
530 | ]
531 | },
532 | {
533 | "cell_type": "markdown",
534 | "id": "77653c16",
535 | "metadata": {},
536 | "source": [
537 | "### (Extra) Task 4: Plot a histogram of depature delays\n",
538 | "\n",
539 | "Tip: Use `Departure delay (Minutes)` column. As there are some rare large outliers, you may choose to restrict dataset to the observations below some threshold.\n"
540 | ]
541 | },
542 | {
543 | "cell_type": "code",
544 | "execution_count": 8,
545 | "id": "3a869999",
546 | "metadata": {},
547 | "outputs": [],
548 | "source": [
549 | "from plotnine import *"
550 | ]
551 | },
552 | {
553 | "cell_type": "code",
554 | "execution_count": 9,
555 | "id": "5a13354b",
556 | "metadata": {},
557 | "outputs": [],
558 | "source": [
559 | "## Add you code here"
560 | ]
561 | }
562 | ],
563 | "metadata": {
564 | "kernelspec": {
565 | "display_name": "Python 3",
566 | "language": "python",
567 | "name": "python3"
568 | },
569 | "language_info": {
570 | "codemirror_mode": {
571 | "name": "ipython",
572 | "version": 3
573 | },
574 | "file_extension": ".py",
575 | "mimetype": "text/x-python",
576 | "name": "python",
577 | "nbconvert_exporter": "python",
578 | "pygments_lexer": "ipython3",
579 | "version": "3.8.10"
580 | }
581 | },
582 | "nbformat": 4,
583 | "nbformat_minor": 5
584 | }
585 |
--------------------------------------------------------------------------------
/lectures/01_introduction.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "c0eebe33",
6 | "metadata": {
7 | "slideshow": {
8 | "slide_type": "slide"
9 | }
10 | },
11 | "source": [
12 | "# Part 1: Introduction\n",
13 | "\n",
14 | "## [Introduction to Python](https://ds3.ai/2022/python.html)\n",
15 | "\n",
16 | "### [Tom Paskhalis](https://tom.paskhal.is/)\n",
17 | "\n",
18 | "##### 2022-07-27\n",
19 | "\n",
20 | "##### [Data Science Summer School 2022](https://ds3.ai/index.html)"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "id": "33ae2156",
26 | "metadata": {
27 | "slideshow": {
28 | "slide_type": "slide"
29 | }
30 | },
31 | "source": [
32 | "\n",
33 | " \n",
34 | "  | \n",
35 | "  | \n",
36 | "
\n",
37 | "
"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "id": "ed1d823e",
43 | "metadata": {
44 | "slideshow": {
45 | "slide_type": "slide"
46 | }
47 | },
48 | "source": [
49 | "\n",
50 | "

\n",
51 | "
\n",
52 | "\n",
53 | "Source: [xkcd](https://xkcd.com/353/)"
54 | ]
55 | },
56 | {
57 | "cell_type": "markdown",
58 | "id": "a5335583",
59 | "metadata": {
60 | "slideshow": {
61 | "slide_type": "slide"
62 | }
63 | },
64 | "source": [
65 | "\n",
66 | "

\n",
67 | "
\n",
68 | "\n",
69 | "Source: [Reddit](https://www.reddit.com/r/datascience/comments/aoacek/yes/)"
70 | ]
71 | },
72 | {
73 | "cell_type": "markdown",
74 | "id": "c6cb304c",
75 | "metadata": {
76 | "slideshow": {
77 | "slide_type": "slide"
78 | }
79 | },
80 | "source": [
81 | "## About me\n",
82 | "\n",
83 | "- Assistant Professor in Political Science and Data Science, [Trinity College Dublin](https://www.tcd.ie/)\n",
84 | " - Before: Postdoctoral Fellow, [New York University](https://www.nyu.edu/)\n",
85 | " - PhD in Social Research Methods, [London School of Economics and Political Science](http://www.lse.ac.uk/)\n",
86 | "- My research:\n",
87 | " - Political communication, social media, interest groups\n",
88 | " - Text analysis, machine learning, record linkage, data visualization\n",
89 | "- Contact\n",
90 | " - [tom.paskhalis@tcd.ie](mailto:tom.paskhalis@tcd.ie)\n",
91 | " - [tom.paskhal.is](https://tom.paskhal.is/)\n",
92 | " - [@tpaskhalis](https://twitter.com/tpaskhalis/)"
93 | ]
94 | },
95 | {
96 | "cell_type": "markdown",
97 | "id": "5e9016b0",
98 | "metadata": {
99 | "slideshow": {
100 | "slide_type": "slide"
101 | }
102 | },
103 | "source": [
104 | "\n",
105 | "

\n",
106 | "
\n",
107 | "\n",
108 | "Source: [Tumblr](https://incorrecttintin.tumblr.com/post/162088281738)"
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "id": "ff57a346",
114 | "metadata": {
115 | "slideshow": {
116 | "slide_type": "slide"
117 | }
118 | },
119 | "source": [
120 | "## About you\n",
121 | "\n",
122 | "\n",
123 | " \n",
124 | "  | \n",
125 | " \n",
126 | " Go to www.menti.com Enter the code: 4525 3763\n",
127 | " | \n",
128 | "
\n",
129 | "
"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "id": "83fccfc1",
135 | "metadata": {
136 | "slideshow": {
137 | "slide_type": "slide"
138 | }
139 | },
140 | "source": [
141 | "## R/Stata/SPSS is great, why learn Python?\n",
142 | "\n",
143 | "- [Python is free and open source](https://github.com/python/cpython)\n",
144 | "- [Python is a truly versatile programming language](https://github.com/readme/nasa-ingenuity-helicopter)\n",
145 | "- [Python offers a great library ecosystem (>300K)](https://pypi.org/)\n",
146 | "- [Python is widely used in the industry](https://www.tiobe.com/tiobe-index/)\n",
147 | "- [Python is well-known outside academia/data science](https://www.economist.com/science-and-technology/2018/07/19/python-has-brought-computer-programming-to-a-vast-new-audience)"
148 | ]
149 | },
150 | {
151 | "cell_type": "markdown",
152 | "id": "abfe3136",
153 | "metadata": {
154 | "slideshow": {
155 | "slide_type": "slide"
156 | }
157 | },
158 | "source": [
159 | "## Popularity of programming languages\n",
160 | "\n",
161 | "\n",
162 | "

\n",
163 | "
\n",
164 | " \n",
165 | "Source: [TIOBE](https://www.tiobe.com/tiobe-index/)"
166 | ]
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "id": "fd915d56",
171 | "metadata": {
172 | "slideshow": {
173 | "slide_type": "slide"
174 | }
175 | },
176 | "source": [
177 | "## Popularity of data analysis software\n",
178 | "\n",
179 | "\n",
180 | "

\n",
181 | "
\n",
182 | "\n",
183 | "Source: [Kaggle 2021 *State of Data Science and Machine Learning* survey](https://www.kaggle.com/kaggle-survey-2021)"
184 | ]
185 | },
186 | {
187 | "cell_type": "markdown",
188 | "id": "c890b62b",
189 | "metadata": {
190 | "slideshow": {
191 | "slide_type": "slide"
192 | }
193 | },
194 | "source": [
195 | "## Python and Development Enviroments\n",
196 | "\n",
197 | "- There is a number of integrated development environments (*IDE*s) available for Python (IDLE, Spyder, PyCharm)\n",
198 | "- As well code editors with Python-specific extensions (Visual Studio Code, Atom, Sublime Text, Vim)\n",
199 | "- Try different ones and choose what works best for you!"
200 | ]
201 | },
202 | {
203 | "cell_type": "markdown",
204 | "id": "6222a9ec",
205 | "metadata": {
206 | "slideshow": {
207 | "slide_type": "slide"
208 | }
209 | },
210 | "source": [
211 | "## Python and Jupyter Notebook\n",
212 | "\n",
213 | "- [Jupyter Notebook](https://jupyter-notebook.readthedocs.io/en/latest/) is language-agnostic web-based interactive computational environment\n",
214 | "- Is available with backends (*kernels*) for different programming languages (**Ju**lia, **Py**thon, **R** = **Jupy**te**r**)\n",
215 | "- Can be used both locally and remotely\n",
216 | "- Good for ad-hoc data analysis and visualization "
217 | ]
218 | },
219 | {
220 | "cell_type": "markdown",
221 | "id": "70fa677c",
222 | "metadata": {
223 | "slideshow": {
224 | "slide_type": "slide"
225 | }
226 | },
227 | "source": [
228 | "## Jupyter Notebook\n",
229 | "\n",
230 | "- Notebooks allow writing, executing and viewing the output of Python code within the same environment\n",
231 | "- All notebook files have `.ipynb` extension for **i**nteractive **py**thon **n**ote**b**ook\n",
232 | "- The main unit of notebook is *cell*, a text input field (Python, Markdown, HTML)\n",
233 | "- Output of a cell can include text, table or figure"
234 | ]
235 | },
236 | {
237 | "cell_type": "markdown",
238 | "id": "305be843",
239 | "metadata": {
240 | "slideshow": {
241 | "slide_type": "slide"
242 | }
243 | },
244 | "source": [
245 | "## Jupyter Notebook online\n",
246 | "\n",
247 | "- For this workshop I recommend using one of the online platforms for working with Jupyter Notebooks:\n",
248 | " - [Google Colab](https://colab.research.google.com/notebooks/intro.ipynb), a cloud platform for hosting Jupyter Notebooks. You need to have a Google account, but it does not require any local installations.\n",
249 | " - [Kaggle Code](https://www.kaggle.com/code), a platform for sharing and exploring data-science-focussed Jupyter Notebooks. Although technically owned by Google, you can register just for Kaggle website."
250 | ]
251 | },
252 | {
253 | "cell_type": "markdown",
254 | "id": "c8a286fd",
255 | "metadata": {
256 | "slideshow": {
257 | "slide_type": "slide"
258 | }
259 | },
260 | "source": [
261 | "## Jupyter Notebook installation\n",
262 | "\n",
263 | "- If you would prefer to install Jupyter Notebook on your local machine, there are two main ways to do this: \n",
264 | " - [pip](https://jupyter.readthedocs.io/en/latest/install/notebook-classic.html#alternative-for-experienced-python-users-installing-jupyter-with-pip)\n",
265 | " - [conda](https://jupyter.readthedocs.io/en/latest/install/notebook-classic.html#installing-jupyter-using-anaconda-and-conda)\n",
266 | "- Unless you have prior experience with Python, I recommend installing [Anaconda](https://www.anaconda.com/products/individual) distribution, which contains all the packages required for this course."
267 | ]
268 | },
269 | {
270 | "cell_type": "markdown",
271 | "id": "6a79ef57",
272 | "metadata": {
273 | "slideshow": {
274 | "slide_type": "slide"
275 | }
276 | },
277 | "source": [
278 | "## Jupyter Notebook demonstration\n",
279 | "\n",
280 | ""
281 | ]
282 | },
283 | {
284 | "cell_type": "markdown",
285 | "id": "881ba5e7",
286 | "metadata": {
287 | "slideshow": {
288 | "slide_type": "slide"
289 | }
290 | },
291 | "source": [
292 | "## Jupyter Notebook demonstration\n",
293 | "\n",
294 | ""
295 | ]
296 | },
297 | {
298 | "cell_type": "markdown",
299 | "id": "94829a20",
300 | "metadata": {
301 | "slideshow": {
302 | "slide_type": "slide"
303 | }
304 | },
305 | "source": [
306 | "## Course outline\n",
307 | "\n",
308 | "| Date | Time (CEST) | Topic |\n",
309 | "|:--------|:--------------|:-------------------------------------------------|\n",
310 | "| 27 July | 15:00-16:45 | Introduction to Python objects and data types |\n",
311 | "| | 16:45-17:00 | Exercise I |\n",
312 | "| | 17:00-17:15 | Break |\n",
313 | "| | 17:15-18:00 | Introduction to Pandas |\n",
314 | "| | 18:00-19:00 | Exploratory data analysis and data visualization |\n",
315 | "| | 19:00-19:15 | Exercise II |\n"
316 | ]
317 | },
318 | {
319 | "cell_type": "markdown",
320 | "id": "a53a487e",
321 | "metadata": {
322 | "slideshow": {
323 | "slide_type": "slide"
324 | }
325 | },
326 | "source": [
327 | "## Materials\n",
328 | "\n",
329 | "- All materials for this workshop can be found: \n",
330 | " - In this GitHub repository: [github.com/tpaskhalis/DS3_Introduction_Python](https://github.com/tpaskhalis/DS3_Introduction_Python)\n",
331 | " - Alternative shortlink: [bit.ly/DS3-Python](https://bit.ly/DS3-Python)\n",
332 | "- For your convenience you might want to choose to clone this repository to your local macihine.\n",
333 | "- It is worth noting that all slides and exercises were created using Python and Jupyter Notebooks."
334 | ]
335 | },
336 | {
337 | "cell_type": "markdown",
338 | "id": "f46c4cbb",
339 | "metadata": {
340 | "slideshow": {
341 | "slide_type": "slide"
342 | }
343 | },
344 | "source": [
345 | "## Additional materials\n",
346 | "\n",
347 | "- There are many great online resources and published books on programming in Python.\n",
348 | "- Some of them also provide a good coverage of using Python for data analysis.\n",
349 | "- Here are some pointers to start from."
350 | ]
351 | },
352 | {
353 | "cell_type": "markdown",
354 | "id": "1b9bf824",
355 | "metadata": {
356 | "slideshow": {
357 | "slide_type": "slide"
358 | }
359 | },
360 | "source": [
361 | "## Books\n",
362 | "\n",
363 | "- Guttag, John. 2021 *Introduction to Computation and Programming Using Python: With Application to Computational Modeling and Understanding Data*. 3rd ed. Cambridge, MA: The MIT Press\n",
364 | "\n",
365 | "- McKinney, Wes. 2017. *Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython*. 2nd ed. Sebastopol, CA: O'Reilly Media\n",
366 | "\n",
367 | "- Sweigart, Al. 2019. *Automate the Boring Stuff with Python*. 2nd ed. San Francisco, CA: No Starch Press"
368 | ]
369 | },
370 | {
371 | "cell_type": "markdown",
372 | "id": "07508e09",
373 | "metadata": {
374 | "slideshow": {
375 | "slide_type": "slide"
376 | }
377 | },
378 | "source": [
379 | "## Online\n",
380 | "\n",
381 | "- [Python For You and Me](https://pymbook.readthedocs.io/en/latest/)\n",
382 | "\n",
383 | "- [Python Wikibook](https://en.wikibooks.org/wiki/Python_Programming)\n",
384 | "\n",
385 | "- [Python 3 Documentation](https://docs.python.org/3/) (intermediate and advanced)"
386 | ]
387 | },
388 | {
389 | "cell_type": "markdown",
390 | "id": "f3e355d4",
391 | "metadata": {
392 | "slideshow": {
393 | "slide_type": "slide"
394 | }
395 | },
396 | "source": [
397 | "## Next\n",
398 | "\n",
399 | "- Basic Python types\n",
400 | "- Operations\n",
401 | "- Object manipulations"
402 | ]
403 | }
404 | ],
405 | "metadata": {
406 | "celltoolbar": "Slideshow",
407 | "kernelspec": {
408 | "display_name": "Python 3",
409 | "language": "python",
410 | "name": "python3"
411 | },
412 | "language_info": {
413 | "codemirror_mode": {
414 | "name": "ipython",
415 | "version": 3
416 | },
417 | "file_extension": ".py",
418 | "mimetype": "text/x-python",
419 | "name": "python",
420 | "nbconvert_exporter": "python",
421 | "pygments_lexer": "ipython3",
422 | "version": "3.8.10"
423 | }
424 | },
425 | "nbformat": 4,
426 | "nbformat_minor": 5
427 | }
428 |
--------------------------------------------------------------------------------
/lectures/02_basics.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "7a2cca06",
6 | "metadata": {
7 | "slideshow": {
8 | "slide_type": "slide"
9 | }
10 | },
11 | "source": [
12 | "# Part 2: Python Basics\n",
13 | "\n",
14 | "## Introduction to Python\n",
15 | "\n",
16 | "### Tom Paskhalis\n",
17 | "\n",
18 | "##### 2022-07-27\n",
19 | "\n",
20 | "##### Data Science Summer School 2022"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "id": "32e8caf3",
26 | "metadata": {
27 | "slideshow": {
28 | "slide_type": "slide"
29 | }
30 | },
31 | "source": [
32 | "## Python background\n",
33 | "\n",
34 | "\n",
35 | " \n",
36 | "  | \n",
37 | "  | \n",
38 | "
\n",
39 | "
\n",
40 | "\n",
41 | "Source: [Guido van Rossum](https://gvanrossum.github.io/), [Python Software Foundation](https://www.python.org/psf-landing/)\n",
42 | "\n",
43 | "- Started as a side-project in 1989 by Guido van Rossum, BDFL (benevolent dictator for life) until 2018.\n",
44 | "- Python 3, first released in 2008, is the current major version\n",
45 | "- Python 2 support stopped on 1 January 2020"
46 | ]
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "id": "91997b5a",
51 | "metadata": {
52 | "slideshow": {
53 | "slide_type": "slide"
54 | }
55 | },
56 | "source": [
57 | "## The Zen of Python"
58 | ]
59 | },
60 | {
61 | "cell_type": "code",
62 | "execution_count": 1,
63 | "id": "3aa5884b",
64 | "metadata": {
65 | "slideshow": {
66 | "slide_type": "fragment"
67 | }
68 | },
69 | "outputs": [
70 | {
71 | "name": "stdout",
72 | "output_type": "stream",
73 | "text": [
74 | "The Zen of Python, by Tim Peters\n",
75 | "\n",
76 | "Beautiful is better than ugly.\n",
77 | "Explicit is better than implicit.\n",
78 | "Simple is better than complex.\n",
79 | "Complex is better than complicated.\n",
80 | "Flat is better than nested.\n",
81 | "Sparse is better than dense.\n",
82 | "Readability counts.\n",
83 | "Special cases aren't special enough to break the rules.\n",
84 | "Although practicality beats purity.\n",
85 | "Errors should never pass silently.\n",
86 | "Unless explicitly silenced.\n",
87 | "In the face of ambiguity, refuse the temptation to guess.\n",
88 | "There should be one-- and preferably only one --obvious way to do it.\n",
89 | "Although that way may not be obvious at first unless you're Dutch.\n",
90 | "Now is better than never.\n",
91 | "Although never is often better than *right* now.\n",
92 | "If the implementation is hard to explain, it's a bad idea.\n",
93 | "If the implementation is easy to explain, it may be a good idea.\n",
94 | "Namespaces are one honking great idea -- let's do more of those!\n"
95 | ]
96 | }
97 | ],
98 | "source": [
99 | "import this"
100 | ]
101 | },
102 | {
103 | "cell_type": "markdown",
104 | "id": "9012a8ae",
105 | "metadata": {
106 | "slideshow": {
107 | "slide_type": "slide"
108 | }
109 | },
110 | "source": [
111 | "## Python basics\n",
112 | "\n",
113 | "- Python is an *intepreted* language (like R and Stata)\n",
114 | "- Every program is executed one *command* (aka *statement*) at a time\n",
115 | "- Which also means that work can be done interactively"
116 | ]
117 | },
118 | {
119 | "cell_type": "code",
120 | "execution_count": 2,
121 | "id": "14e8578c",
122 | "metadata": {
123 | "slideshow": {
124 | "slide_type": "fragment"
125 | }
126 | },
127 | "outputs": [
128 | {
129 | "name": "stdout",
130 | "output_type": "stream",
131 | "text": [
132 | "Hello World!\n"
133 | ]
134 | }
135 | ],
136 | "source": [
137 | "print(\"Hello World!\")"
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "id": "326046f6",
143 | "metadata": {
144 | "slideshow": {
145 | "slide_type": "slide"
146 | }
147 | },
148 | "source": [
149 | "## Python conceptual hierarchy\n",
150 | "\n",
151 | "Python programs can be decomposed into modules, statements, expressions, and objects, as follows:\n",
152 | "\n",
153 | "1. *Programs* are composed of *modules*\n",
154 | "2. *Modules* contain *statements*\n",
155 | "3. *Statements* contain *expressions*\n",
156 | "4. *Expressions* create and process *objects*"
157 | ]
158 | },
159 | {
160 | "cell_type": "markdown",
161 | "id": "48b4d423",
162 | "metadata": {
163 | "slideshow": {
164 | "slide_type": "slide"
165 | }
166 | },
167 | "source": [
168 | "## Python objects\n",
169 | "\n",
170 | "- Everything that Python operates on is an *object*\n",
171 | "- This includes numbers, strings, data structures, functions, etc.\n",
172 | "- Eact object has a *type* (e.g. string or function) and internal data\n",
173 | "- Objects can be *mutable* (e.g. list) and *immutable* (e.g. string)"
174 | ]
175 | },
176 | {
177 | "cell_type": "markdown",
178 | "id": "bec7fefe",
179 | "metadata": {
180 | "slideshow": {
181 | "slide_type": "slide"
182 | }
183 | },
184 | "source": [
185 | "## Operators\n",
186 | "\n",
187 | "*Objects* and *operators* are combined to form *expressions*. Key *operators* are:\n",
188 | "\n",
189 | "- Arithmetic (`+`, `-`, `*`, `**`, `/`, `//`, `%`)\n",
190 | "- Boolean (`and`, `or`, `not`)\n",
191 | "- Relational (`==`, `!=`, `>`, `>=`, `<`, `<=`)\n",
192 | "- Assignment (`=`, `+=`, `-=`, `*=`, `/=`)\n",
193 | "- Membership (`in`)"
194 | ]
195 | },
196 | {
197 | "cell_type": "markdown",
198 | "id": "202a9962",
199 | "metadata": {
200 | "slideshow": {
201 | "slide_type": "slide"
202 | }
203 | },
204 | "source": [
205 | "## Basic mathematical operations in Python"
206 | ]
207 | },
208 | {
209 | "cell_type": "code",
210 | "execution_count": 3,
211 | "id": "389a459d",
212 | "metadata": {
213 | "slideshow": {
214 | "slide_type": "fragment"
215 | }
216 | },
217 | "outputs": [
218 | {
219 | "data": {
220 | "text/plain": [
221 | "2"
222 | ]
223 | },
224 | "execution_count": 3,
225 | "metadata": {},
226 | "output_type": "execute_result"
227 | }
228 | ],
229 | "source": [
230 | "1 + 1"
231 | ]
232 | },
233 | {
234 | "cell_type": "code",
235 | "execution_count": 4,
236 | "id": "964a1983",
237 | "metadata": {
238 | "slideshow": {
239 | "slide_type": "fragment"
240 | }
241 | },
242 | "outputs": [
243 | {
244 | "data": {
245 | "text/plain": [
246 | "2"
247 | ]
248 | },
249 | "execution_count": 4,
250 | "metadata": {},
251 | "output_type": "execute_result"
252 | }
253 | ],
254 | "source": [
255 | "5 - 3"
256 | ]
257 | },
258 | {
259 | "cell_type": "code",
260 | "execution_count": 5,
261 | "id": "96a3782a",
262 | "metadata": {
263 | "slideshow": {
264 | "slide_type": "fragment"
265 | }
266 | },
267 | "outputs": [
268 | {
269 | "data": {
270 | "text/plain": [
271 | "3.0"
272 | ]
273 | },
274 | "execution_count": 5,
275 | "metadata": {},
276 | "output_type": "execute_result"
277 | }
278 | ],
279 | "source": [
280 | "6 / 2"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 6,
286 | "id": "9dbed485",
287 | "metadata": {
288 | "slideshow": {
289 | "slide_type": "fragment"
290 | }
291 | },
292 | "outputs": [
293 | {
294 | "data": {
295 | "text/plain": [
296 | "16"
297 | ]
298 | },
299 | "execution_count": 6,
300 | "metadata": {},
301 | "output_type": "execute_result"
302 | }
303 | ],
304 | "source": [
305 | "4 * 4"
306 | ]
307 | },
308 | {
309 | "cell_type": "code",
310 | "execution_count": 7,
311 | "id": "27ac384b",
312 | "metadata": {
313 | "slideshow": {
314 | "slide_type": "fragment"
315 | }
316 | },
317 | "outputs": [
318 | {
319 | "data": {
320 | "text/plain": [
321 | "16"
322 | ]
323 | },
324 | "execution_count": 7,
325 | "metadata": {},
326 | "output_type": "execute_result"
327 | }
328 | ],
329 | "source": [
330 | "# Exponentiation <- Python comments start with #\n",
331 | "2 ** 4"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "id": "9ee768e6",
337 | "metadata": {
338 | "slideshow": {
339 | "slide_type": "slide"
340 | }
341 | },
342 | "source": [
343 | "## Basic logical operations in Python"
344 | ]
345 | },
346 | {
347 | "cell_type": "code",
348 | "execution_count": 8,
349 | "id": "cfdf7738",
350 | "metadata": {
351 | "slideshow": {
352 | "slide_type": "fragment"
353 | }
354 | },
355 | "outputs": [
356 | {
357 | "data": {
358 | "text/plain": [
359 | "True"
360 | ]
361 | },
362 | "execution_count": 8,
363 | "metadata": {},
364 | "output_type": "execute_result"
365 | }
366 | ],
367 | "source": [
368 | "3 != 1 # Not equal"
369 | ]
370 | },
371 | {
372 | "cell_type": "code",
373 | "execution_count": 9,
374 | "id": "2c75af08",
375 | "metadata": {
376 | "slideshow": {
377 | "slide_type": "fragment"
378 | }
379 | },
380 | "outputs": [
381 | {
382 | "data": {
383 | "text/plain": [
384 | "False"
385 | ]
386 | },
387 | "execution_count": 9,
388 | "metadata": {},
389 | "output_type": "execute_result"
390 | }
391 | ],
392 | "source": [
393 | "3 > 3 # Greater than"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 10,
399 | "id": "3e46973b",
400 | "metadata": {
401 | "slideshow": {
402 | "slide_type": "fragment"
403 | }
404 | },
405 | "outputs": [
406 | {
407 | "data": {
408 | "text/plain": [
409 | "True"
410 | ]
411 | },
412 | "execution_count": 10,
413 | "metadata": {},
414 | "output_type": "execute_result"
415 | }
416 | ],
417 | "source": [
418 | "3 >= 3 # Greater than or equal"
419 | ]
420 | },
421 | {
422 | "cell_type": "code",
423 | "execution_count": 11,
424 | "id": "ab9627a2",
425 | "metadata": {
426 | "slideshow": {
427 | "slide_type": "fragment"
428 | }
429 | },
430 | "outputs": [
431 | {
432 | "data": {
433 | "text/plain": [
434 | "True"
435 | ]
436 | },
437 | "execution_count": 11,
438 | "metadata": {},
439 | "output_type": "execute_result"
440 | }
441 | ],
442 | "source": [
443 | "False or True # True if either first or second operand is True, False otherwise"
444 | ]
445 | },
446 | {
447 | "cell_type": "code",
448 | "execution_count": 12,
449 | "id": "5de3615f",
450 | "metadata": {
451 | "slideshow": {
452 | "slide_type": "fragment"
453 | }
454 | },
455 | "outputs": [
456 | {
457 | "data": {
458 | "text/plain": [
459 | "True"
460 | ]
461 | },
462 | "execution_count": 12,
463 | "metadata": {},
464 | "output_type": "execute_result"
465 | }
466 | ],
467 | "source": [
468 | "3 > 3 or 3 >= 3 # Combining 3 Boolean expressions"
469 | ]
470 | },
471 | {
472 | "cell_type": "markdown",
473 | "id": "8ae80f52",
474 | "metadata": {
475 | "slideshow": {
476 | "slide_type": "slide"
477 | }
478 | },
479 | "source": [
480 | "## Assignment operations\n",
481 | "\n",
482 | "- Assignments create object references.\n",
483 | "- *Target* (or *name*) on the left is assigned to *object* on the right."
484 | ]
485 | },
486 | {
487 | "cell_type": "code",
488 | "execution_count": 13,
489 | "id": "6770d1f4",
490 | "metadata": {
491 | "slideshow": {
492 | "slide_type": "fragment"
493 | }
494 | },
495 | "outputs": [],
496 | "source": [
497 | "x = 3"
498 | ]
499 | },
500 | {
501 | "cell_type": "code",
502 | "execution_count": 14,
503 | "id": "3e67a255",
504 | "metadata": {
505 | "slideshow": {
506 | "slide_type": "fragment"
507 | }
508 | },
509 | "outputs": [
510 | {
511 | "data": {
512 | "text/plain": [
513 | "3"
514 | ]
515 | },
516 | "execution_count": 14,
517 | "metadata": {},
518 | "output_type": "execute_result"
519 | }
520 | ],
521 | "source": [
522 | "x"
523 | ]
524 | },
525 | {
526 | "cell_type": "code",
527 | "execution_count": 15,
528 | "id": "15fdef9a",
529 | "metadata": {
530 | "slideshow": {
531 | "slide_type": "fragment"
532 | }
533 | },
534 | "outputs": [],
535 | "source": [
536 | "x += 2 # Increment assignment, equivalent to x = x + 2"
537 | ]
538 | },
539 | {
540 | "cell_type": "code",
541 | "execution_count": 16,
542 | "id": "99621b62",
543 | "metadata": {
544 | "slideshow": {
545 | "slide_type": "fragment"
546 | }
547 | },
548 | "outputs": [
549 | {
550 | "data": {
551 | "text/plain": [
552 | "5"
553 | ]
554 | },
555 | "execution_count": 16,
556 | "metadata": {},
557 | "output_type": "execute_result"
558 | }
559 | ],
560 | "source": [
561 | "x"
562 | ]
563 | },
564 | {
565 | "cell_type": "markdown",
566 | "id": "31c49cf1",
567 | "metadata": {
568 | "slideshow": {
569 | "slide_type": "slide"
570 | }
571 | },
572 | "source": [
573 | "## Assignment vs Comparison Operators\n",
574 | "\n",
575 | "As `=` (assignment) and `==` (equality comparison) operators appear very similar, they sometime can create confusion."
576 | ]
577 | },
578 | {
579 | "cell_type": "code",
580 | "execution_count": 17,
581 | "id": "9a8d1438",
582 | "metadata": {
583 | "slideshow": {
584 | "slide_type": "fragment"
585 | }
586 | },
587 | "outputs": [],
588 | "source": [
589 | "x = 3"
590 | ]
591 | },
592 | {
593 | "cell_type": "code",
594 | "execution_count": 18,
595 | "id": "4dc74000",
596 | "metadata": {
597 | "slideshow": {
598 | "slide_type": "fragment"
599 | }
600 | },
601 | "outputs": [
602 | {
603 | "data": {
604 | "text/plain": [
605 | "3"
606 | ]
607 | },
608 | "execution_count": 18,
609 | "metadata": {},
610 | "output_type": "execute_result"
611 | }
612 | ],
613 | "source": [
614 | "x"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "execution_count": 19,
620 | "id": "2890797e",
621 | "metadata": {
622 | "slideshow": {
623 | "slide_type": "fragment"
624 | }
625 | },
626 | "outputs": [
627 | {
628 | "data": {
629 | "text/plain": [
630 | "True"
631 | ]
632 | },
633 | "execution_count": 19,
634 | "metadata": {},
635 | "output_type": "execute_result"
636 | }
637 | ],
638 | "source": [
639 | "x == 3"
640 | ]
641 | },
642 | {
643 | "cell_type": "markdown",
644 | "id": "0764c987",
645 | "metadata": {
646 | "slideshow": {
647 | "slide_type": "slide"
648 | }
649 | },
650 | "source": [
651 | "## Membership operations\n",
652 | "\n",
653 | "Operator `in` returns `True` if an object of the left side is in a sequence on the right."
654 | ]
655 | },
656 | {
657 | "cell_type": "code",
658 | "execution_count": 20,
659 | "id": "208af6b3",
660 | "metadata": {
661 | "slideshow": {
662 | "slide_type": "fragment"
663 | }
664 | },
665 | "outputs": [
666 | {
667 | "data": {
668 | "text/plain": [
669 | "True"
670 | ]
671 | },
672 | "execution_count": 20,
673 | "metadata": {},
674 | "output_type": "execute_result"
675 | }
676 | ],
677 | "source": [
678 | "'a' in 'abc'"
679 | ]
680 | },
681 | {
682 | "cell_type": "code",
683 | "execution_count": 21,
684 | "id": "1529ba70",
685 | "metadata": {
686 | "slideshow": {
687 | "slide_type": "fragment"
688 | }
689 | },
690 | "outputs": [
691 | {
692 | "data": {
693 | "text/plain": [
694 | "False"
695 | ]
696 | },
697 | "execution_count": 21,
698 | "metadata": {},
699 | "output_type": "execute_result"
700 | }
701 | ],
702 | "source": [
703 | "4 in [1, 2, 3] # [1,2,3] is a list"
704 | ]
705 | },
706 | {
707 | "cell_type": "code",
708 | "execution_count": 22,
709 | "id": "141d9fdd",
710 | "metadata": {
711 | "slideshow": {
712 | "slide_type": "fragment"
713 | }
714 | },
715 | "outputs": [
716 | {
717 | "data": {
718 | "text/plain": [
719 | "True"
720 | ]
721 | },
722 | "execution_count": 22,
723 | "metadata": {},
724 | "output_type": "execute_result"
725 | }
726 | ],
727 | "source": [
728 | "4 not in [1, 2, 3]"
729 | ]
730 | },
731 | {
732 | "cell_type": "markdown",
733 | "id": "37c3ad50",
734 | "metadata": {
735 | "slideshow": {
736 | "slide_type": "slide"
737 | }
738 | },
739 | "source": [
740 | "## Object types\n",
741 | "\n",
742 | "Python objects can have *scalar* and *non-scalar* types. Scalar objects are indivisible.\n",
743 | "\n",
744 | "4 main types of scalar objects in Python:\n",
745 | "\n",
746 | "- Integer (`int`)\n",
747 | "- Real number (`float`)\n",
748 | "- Boolean (`bool`)\n",
749 | "- Null value (`None`)"
750 | ]
751 | },
752 | {
753 | "cell_type": "markdown",
754 | "id": "6825a934",
755 | "metadata": {
756 | "slideshow": {
757 | "slide_type": "slide"
758 | }
759 | },
760 | "source": [
761 | "## Scalar types"
762 | ]
763 | },
764 | {
765 | "cell_type": "code",
766 | "execution_count": 23,
767 | "id": "a7b88274",
768 | "metadata": {
769 | "slideshow": {
770 | "slide_type": "fragment"
771 | }
772 | },
773 | "outputs": [
774 | {
775 | "data": {
776 | "text/plain": [
777 | "int"
778 | ]
779 | },
780 | "execution_count": 23,
781 | "metadata": {},
782 | "output_type": "execute_result"
783 | }
784 | ],
785 | "source": [
786 | "type(7)"
787 | ]
788 | },
789 | {
790 | "cell_type": "code",
791 | "execution_count": 24,
792 | "id": "b0434a62",
793 | "metadata": {
794 | "slideshow": {
795 | "slide_type": "fragment"
796 | }
797 | },
798 | "outputs": [
799 | {
800 | "data": {
801 | "text/plain": [
802 | "float"
803 | ]
804 | },
805 | "execution_count": 24,
806 | "metadata": {},
807 | "output_type": "execute_result"
808 | }
809 | ],
810 | "source": [
811 | "type(3.14)"
812 | ]
813 | },
814 | {
815 | "cell_type": "code",
816 | "execution_count": 25,
817 | "id": "2e72be37",
818 | "metadata": {
819 | "slideshow": {
820 | "slide_type": "fragment"
821 | }
822 | },
823 | "outputs": [
824 | {
825 | "data": {
826 | "text/plain": [
827 | "bool"
828 | ]
829 | },
830 | "execution_count": 25,
831 | "metadata": {},
832 | "output_type": "execute_result"
833 | }
834 | ],
835 | "source": [
836 | "type(True)"
837 | ]
838 | },
839 | {
840 | "cell_type": "code",
841 | "execution_count": 26,
842 | "id": "1401ca30",
843 | "metadata": {
844 | "slideshow": {
845 | "slide_type": "fragment"
846 | }
847 | },
848 | "outputs": [
849 | {
850 | "data": {
851 | "text/plain": [
852 | "NoneType"
853 | ]
854 | },
855 | "execution_count": 26,
856 | "metadata": {},
857 | "output_type": "execute_result"
858 | }
859 | ],
860 | "source": [
861 | "type(None)"
862 | ]
863 | },
864 | {
865 | "cell_type": "code",
866 | "execution_count": 27,
867 | "id": "84e4918a",
868 | "metadata": {
869 | "slideshow": {
870 | "slide_type": "fragment"
871 | }
872 | },
873 | "outputs": [
874 | {
875 | "data": {
876 | "text/plain": [
877 | "3"
878 | ]
879 | },
880 | "execution_count": 27,
881 | "metadata": {},
882 | "output_type": "execute_result"
883 | }
884 | ],
885 | "source": [
886 | "int(3.14) # Scalar type conversion (casting)"
887 | ]
888 | },
889 | {
890 | "cell_type": "markdown",
891 | "id": "20c22fcc",
892 | "metadata": {
893 | "slideshow": {
894 | "slide_type": "slide"
895 | }
896 | },
897 | "source": [
898 | "## Non-scalar types\n",
899 | "\n",
900 | "In contrast to scalars, non-scalar objects, *sequences*, have some internal structure. This allows indexing, slicing and other interesting operations.\n",
901 | "\n",
902 | "Most common sequences in Python are:\n",
903 | "\n",
904 | "- String (`str`) - *immutable* ordered sequence of characters\n",
905 | "- Tuple (`tuple`) - *immutable* ordered sequence of elements\n",
906 | "- List (`list`) - *mutable* ordered sequence of elements\n",
907 | "- Set (`set`) - *mutable* unordered collection of unique elements\n",
908 | "- Dictionary (`dict`) - *mutable* unordered collection of key-value pairs"
909 | ]
910 | },
911 | {
912 | "cell_type": "markdown",
913 | "id": "57ff2191",
914 | "metadata": {
915 | "slideshow": {
916 | "slide_type": "slide"
917 | }
918 | },
919 | "source": [
920 | "## Examples of non-scalar types"
921 | ]
922 | },
923 | {
924 | "cell_type": "code",
925 | "execution_count": 28,
926 | "id": "a7236a16",
927 | "metadata": {
928 | "slideshow": {
929 | "slide_type": "fragment"
930 | }
931 | },
932 | "outputs": [],
933 | "source": [
934 | "s = 'time flies like a banana'\n",
935 | "t = (0, 'one', 1, 2)\n",
936 | "l = [0, 'one', 1, 2]\n",
937 | "o = {'apple', 'banana', 'watermelon'}\n",
938 | "d = {'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0}"
939 | ]
940 | },
941 | {
942 | "cell_type": "code",
943 | "execution_count": 29,
944 | "id": "3fc9d3af",
945 | "metadata": {
946 | "slideshow": {
947 | "slide_type": "fragment"
948 | }
949 | },
950 | "outputs": [
951 | {
952 | "data": {
953 | "text/plain": [
954 | "str"
955 | ]
956 | },
957 | "execution_count": 29,
958 | "metadata": {},
959 | "output_type": "execute_result"
960 | }
961 | ],
962 | "source": [
963 | "type(s)"
964 | ]
965 | },
966 | {
967 | "cell_type": "code",
968 | "execution_count": 30,
969 | "id": "f8515498",
970 | "metadata": {
971 | "slideshow": {
972 | "slide_type": "fragment"
973 | }
974 | },
975 | "outputs": [
976 | {
977 | "data": {
978 | "text/plain": [
979 | "tuple"
980 | ]
981 | },
982 | "execution_count": 30,
983 | "metadata": {},
984 | "output_type": "execute_result"
985 | }
986 | ],
987 | "source": [
988 | "type(t)"
989 | ]
990 | },
991 | {
992 | "cell_type": "code",
993 | "execution_count": 31,
994 | "id": "497fb4dd",
995 | "metadata": {
996 | "slideshow": {
997 | "slide_type": "fragment"
998 | }
999 | },
1000 | "outputs": [
1001 | {
1002 | "data": {
1003 | "text/plain": [
1004 | "list"
1005 | ]
1006 | },
1007 | "execution_count": 31,
1008 | "metadata": {},
1009 | "output_type": "execute_result"
1010 | }
1011 | ],
1012 | "source": [
1013 | "type(l)"
1014 | ]
1015 | },
1016 | {
1017 | "cell_type": "code",
1018 | "execution_count": 32,
1019 | "id": "19c72bae",
1020 | "metadata": {
1021 | "slideshow": {
1022 | "slide_type": "fragment"
1023 | }
1024 | },
1025 | "outputs": [
1026 | {
1027 | "data": {
1028 | "text/plain": [
1029 | "set"
1030 | ]
1031 | },
1032 | "execution_count": 32,
1033 | "metadata": {},
1034 | "output_type": "execute_result"
1035 | }
1036 | ],
1037 | "source": [
1038 | "type(o)"
1039 | ]
1040 | },
1041 | {
1042 | "cell_type": "code",
1043 | "execution_count": 33,
1044 | "id": "eac46394",
1045 | "metadata": {
1046 | "slideshow": {
1047 | "slide_type": "fragment"
1048 | }
1049 | },
1050 | "outputs": [
1051 | {
1052 | "data": {
1053 | "text/plain": [
1054 | "dict"
1055 | ]
1056 | },
1057 | "execution_count": 33,
1058 | "metadata": {},
1059 | "output_type": "execute_result"
1060 | }
1061 | ],
1062 | "source": [
1063 | "type(d)"
1064 | ]
1065 | },
1066 | {
1067 | "cell_type": "markdown",
1068 | "id": "1b0f4d39",
1069 | "metadata": {
1070 | "slideshow": {
1071 | "slide_type": "slide"
1072 | }
1073 | },
1074 | "source": [
1075 | "## Indexing and subsetting in Python\n",
1076 | "\n",
1077 | "- *Indexing* can be used to subset individual elements from a sequence\n",
1078 | "- *Slicing* can be used to extract sub-sequence of arbitrary length\n",
1079 | "- Use square brackets `[]` to supply the index (indices) of elements:\n",
1080 | "\n",
1081 | "```\n",
1082 | "object[index]\n",
1083 | "```"
1084 | ]
1085 | },
1086 | {
1087 | "cell_type": "markdown",
1088 | "id": "8896b49e",
1089 | "metadata": {
1090 | "slideshow": {
1091 | "slide_type": "slide"
1092 | }
1093 | },
1094 | "source": [
1095 | "## Indexing in Python starts from 0\n",
1096 | "\n",
1097 | "\n",
1098 | "

\n",
1099 | "
\n",
1100 | "\n",
1101 | "Source: [xkcd](https://xkcd.com/163/) \n",
1102 | "Extra: [Why Python uses 0-based indexing by Guido van Rossum](https://python-history.blogspot.com/2013/10/why-python-uses-0-based-indexing.html) \n",
1103 | "Extra: [Why numbering should start at zero by Edsger Dijkstra](https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html)"
1104 | ]
1105 | },
1106 | {
1107 | "cell_type": "markdown",
1108 | "id": "6c391c09",
1109 | "metadata": {
1110 | "slideshow": {
1111 | "slide_type": "slide"
1112 | }
1113 | },
1114 | "source": [
1115 | "## Strings"
1116 | ]
1117 | },
1118 | {
1119 | "cell_type": "code",
1120 | "execution_count": 34,
1121 | "id": "0e003be1",
1122 | "metadata": {
1123 | "slideshow": {
1124 | "slide_type": "fragment"
1125 | }
1126 | },
1127 | "outputs": [
1128 | {
1129 | "data": {
1130 | "text/plain": [
1131 | "'time flies like a banana'"
1132 | ]
1133 | },
1134 | "execution_count": 34,
1135 | "metadata": {},
1136 | "output_type": "execute_result"
1137 | }
1138 | ],
1139 | "source": [
1140 | "s"
1141 | ]
1142 | },
1143 | {
1144 | "cell_type": "code",
1145 | "execution_count": 35,
1146 | "id": "737ab1e4",
1147 | "metadata": {
1148 | "slideshow": {
1149 | "slide_type": "fragment"
1150 | }
1151 | },
1152 | "outputs": [
1153 | {
1154 | "data": {
1155 | "text/plain": [
1156 | "24"
1157 | ]
1158 | },
1159 | "execution_count": 35,
1160 | "metadata": {},
1161 | "output_type": "execute_result"
1162 | }
1163 | ],
1164 | "source": [
1165 | "len(s) # length of string (including whitespaces)"
1166 | ]
1167 | },
1168 | {
1169 | "cell_type": "code",
1170 | "execution_count": 36,
1171 | "id": "20aca124",
1172 | "metadata": {
1173 | "slideshow": {
1174 | "slide_type": "fragment"
1175 | }
1176 | },
1177 | "outputs": [
1178 | {
1179 | "data": {
1180 | "text/plain": [
1181 | "'t'"
1182 | ]
1183 | },
1184 | "execution_count": 36,
1185 | "metadata": {},
1186 | "output_type": "execute_result"
1187 | }
1188 | ],
1189 | "source": [
1190 | "s[0] # Subset 1st element (indexing in Python starts from zero!)"
1191 | ]
1192 | },
1193 | {
1194 | "cell_type": "code",
1195 | "execution_count": 37,
1196 | "id": "863aa15d",
1197 | "metadata": {
1198 | "slideshow": {
1199 | "slide_type": "fragment"
1200 | }
1201 | },
1202 | "outputs": [
1203 | {
1204 | "data": {
1205 | "text/plain": [
1206 | "'flies like a banana'"
1207 | ]
1208 | },
1209 | "execution_count": 37,
1210 | "metadata": {},
1211 | "output_type": "execute_result"
1212 | }
1213 | ],
1214 | "source": [
1215 | "s[5:] # Subset all elements starting from 6th"
1216 | ]
1217 | },
1218 | {
1219 | "cell_type": "code",
1220 | "execution_count": 38,
1221 | "id": "0c6467c5",
1222 | "metadata": {
1223 | "slideshow": {
1224 | "slide_type": "fragment"
1225 | }
1226 | },
1227 | "outputs": [
1228 | {
1229 | "data": {
1230 | "text/plain": [
1231 | "'time flies like a banana!'"
1232 | ]
1233 | },
1234 | "execution_count": 38,
1235 | "metadata": {},
1236 | "output_type": "execute_result"
1237 | }
1238 | ],
1239 | "source": [
1240 | "s + '!' # Strings can be concatenated together"
1241 | ]
1242 | },
1243 | {
1244 | "cell_type": "markdown",
1245 | "id": "8c085520",
1246 | "metadata": {
1247 | "slideshow": {
1248 | "slide_type": "slide"
1249 | }
1250 | },
1251 | "source": [
1252 | "## Objects have methods\n",
1253 | "\n",
1254 | "- Python objects of built-in types have *methods* associated with them\n",
1255 | "- They can be thought of function-like objects\n",
1256 | "- However, their syntax is `object.method()` as opposed to `function(object)`"
1257 | ]
1258 | },
1259 | {
1260 | "cell_type": "code",
1261 | "execution_count": 39,
1262 | "id": "e1c11774",
1263 | "metadata": {
1264 | "slideshow": {
1265 | "slide_type": "fragment"
1266 | }
1267 | },
1268 | "outputs": [
1269 | {
1270 | "data": {
1271 | "text/plain": [
1272 | "24"
1273 | ]
1274 | },
1275 | "execution_count": 39,
1276 | "metadata": {},
1277 | "output_type": "execute_result"
1278 | }
1279 | ],
1280 | "source": [
1281 | "len(s) # Function"
1282 | ]
1283 | },
1284 | {
1285 | "cell_type": "code",
1286 | "execution_count": 40,
1287 | "id": "cb92a73d",
1288 | "metadata": {
1289 | "slideshow": {
1290 | "slide_type": "fragment"
1291 | }
1292 | },
1293 | "outputs": [
1294 | {
1295 | "data": {
1296 | "text/plain": [
1297 | "'TIME FLIES LIKE A BANANA'"
1298 | ]
1299 | },
1300 | "execution_count": 40,
1301 | "metadata": {},
1302 | "output_type": "execute_result"
1303 | }
1304 | ],
1305 | "source": [
1306 | "s.upper() # Method (makes string upper-case)"
1307 | ]
1308 | },
1309 | {
1310 | "cell_type": "markdown",
1311 | "id": "e37bd549",
1312 | "metadata": {
1313 | "slideshow": {
1314 | "slide_type": "slide"
1315 | }
1316 | },
1317 | "source": [
1318 | "## String methods\n",
1319 | "\n",
1320 | "Some examples of methods associated with strings. More details [here](https://docs.python.org/3/library/stdtypes.html#string-methods)."
1321 | ]
1322 | },
1323 | {
1324 | "cell_type": "code",
1325 | "execution_count": 41,
1326 | "id": "2ca8ffc5",
1327 | "metadata": {
1328 | "slideshow": {
1329 | "slide_type": "fragment"
1330 | }
1331 | },
1332 | "outputs": [
1333 | {
1334 | "data": {
1335 | "text/plain": [
1336 | "'Time flies like a banana'"
1337 | ]
1338 | },
1339 | "execution_count": 41,
1340 | "metadata": {},
1341 | "output_type": "execute_result"
1342 | }
1343 | ],
1344 | "source": [
1345 | "s.capitalize() # Note that only the first character gets capitalized"
1346 | ]
1347 | },
1348 | {
1349 | "cell_type": "code",
1350 | "execution_count": 42,
1351 | "id": "52f35a24",
1352 | "metadata": {
1353 | "slideshow": {
1354 | "slide_type": "fragment"
1355 | }
1356 | },
1357 | "outputs": [
1358 | {
1359 | "data": {
1360 | "text/plain": [
1361 | "['time', 'flies', 'like', 'a', 'banana']"
1362 | ]
1363 | },
1364 | "execution_count": 42,
1365 | "metadata": {},
1366 | "output_type": "execute_result"
1367 | }
1368 | ],
1369 | "source": [
1370 | "s.split(sep = ' ') # Here we supply an argument 'sep' to our methods call"
1371 | ]
1372 | },
1373 | {
1374 | "cell_type": "code",
1375 | "execution_count": 43,
1376 | "id": "e08225cb",
1377 | "metadata": {
1378 | "slideshow": {
1379 | "slide_type": "fragment"
1380 | }
1381 | },
1382 | "outputs": [
1383 | {
1384 | "data": {
1385 | "text/plain": [
1386 | "'time-flies-like-a-banana'"
1387 | ]
1388 | },
1389 | "execution_count": 43,
1390 | "metadata": {},
1391 | "output_type": "execute_result"
1392 | }
1393 | ],
1394 | "source": [
1395 | "s.replace(' ', '-') # Arguments can also be matched by position, not just name"
1396 | ]
1397 | },
1398 | {
1399 | "cell_type": "code",
1400 | "execution_count": 44,
1401 | "id": "e04e2333",
1402 | "metadata": {
1403 | "slideshow": {
1404 | "slide_type": "fragment"
1405 | }
1406 | },
1407 | "outputs": [
1408 | {
1409 | "data": {
1410 | "text/plain": [
1411 | "'time-flies-like-a-banana'"
1412 | ]
1413 | },
1414 | "execution_count": 44,
1415 | "metadata": {},
1416 | "output_type": "execute_result"
1417 | }
1418 | ],
1419 | "source": [
1420 | "'-'.join(s.split(sep = ' ')) # Methods calls can be nested within each other"
1421 | ]
1422 | },
1423 | {
1424 | "cell_type": "markdown",
1425 | "id": "95f8e144",
1426 | "metadata": {
1427 | "slideshow": {
1428 | "slide_type": "slide"
1429 | }
1430 | },
1431 | "source": [
1432 | "## Tuples"
1433 | ]
1434 | },
1435 | {
1436 | "cell_type": "code",
1437 | "execution_count": 45,
1438 | "id": "e6c063e4",
1439 | "metadata": {
1440 | "slideshow": {
1441 | "slide_type": "fragment"
1442 | }
1443 | },
1444 | "outputs": [
1445 | {
1446 | "data": {
1447 | "text/plain": [
1448 | "(0, 'one', 1, 2)"
1449 | ]
1450 | },
1451 | "execution_count": 45,
1452 | "metadata": {},
1453 | "output_type": "execute_result"
1454 | }
1455 | ],
1456 | "source": [
1457 | "t # Tuples can contain elements of different types"
1458 | ]
1459 | },
1460 | {
1461 | "cell_type": "code",
1462 | "execution_count": 46,
1463 | "id": "020e819d",
1464 | "metadata": {
1465 | "slideshow": {
1466 | "slide_type": "fragment"
1467 | }
1468 | },
1469 | "outputs": [
1470 | {
1471 | "data": {
1472 | "text/plain": [
1473 | "4"
1474 | ]
1475 | },
1476 | "execution_count": 46,
1477 | "metadata": {},
1478 | "output_type": "execute_result"
1479 | }
1480 | ],
1481 | "source": [
1482 | "len(t)"
1483 | ]
1484 | },
1485 | {
1486 | "cell_type": "code",
1487 | "execution_count": 47,
1488 | "id": "45a348b1",
1489 | "metadata": {
1490 | "slideshow": {
1491 | "slide_type": "fragment"
1492 | }
1493 | },
1494 | "outputs": [
1495 | {
1496 | "data": {
1497 | "text/plain": [
1498 | "('one', 1, 2)"
1499 | ]
1500 | },
1501 | "execution_count": 47,
1502 | "metadata": {},
1503 | "output_type": "execute_result"
1504 | }
1505 | ],
1506 | "source": [
1507 | "t[1:]"
1508 | ]
1509 | },
1510 | {
1511 | "cell_type": "code",
1512 | "execution_count": 48,
1513 | "id": "351391f8",
1514 | "metadata": {
1515 | "slideshow": {
1516 | "slide_type": "fragment"
1517 | }
1518 | },
1519 | "outputs": [
1520 | {
1521 | "data": {
1522 | "text/plain": [
1523 | "(0, 'one', 1, 2, 'three', 5)"
1524 | ]
1525 | },
1526 | "execution_count": 48,
1527 | "metadata": {},
1528 | "output_type": "execute_result"
1529 | }
1530 | ],
1531 | "source": [
1532 | "t + ('three', 5) # Like strings tuples can be concatenated"
1533 | ]
1534 | },
1535 | {
1536 | "cell_type": "markdown",
1537 | "id": "836b0bf8",
1538 | "metadata": {
1539 | "slideshow": {
1540 | "slide_type": "slide"
1541 | }
1542 | },
1543 | "source": [
1544 | "## Lists"
1545 | ]
1546 | },
1547 | {
1548 | "cell_type": "code",
1549 | "execution_count": 49,
1550 | "id": "cd9badda",
1551 | "metadata": {
1552 | "slideshow": {
1553 | "slide_type": "fragment"
1554 | }
1555 | },
1556 | "outputs": [
1557 | {
1558 | "data": {
1559 | "text/plain": [
1560 | "[0, 'one', 1, 2]"
1561 | ]
1562 | },
1563 | "execution_count": 49,
1564 | "metadata": {},
1565 | "output_type": "execute_result"
1566 | }
1567 | ],
1568 | "source": [
1569 | "l # Like tuples lists can contain elements of different types"
1570 | ]
1571 | },
1572 | {
1573 | "cell_type": "code",
1574 | "execution_count": 50,
1575 | "id": "9b52e529",
1576 | "metadata": {
1577 | "slideshow": {
1578 | "slide_type": "fragment"
1579 | }
1580 | },
1581 | "outputs": [],
1582 | "source": [
1583 | "l[1] = 1 # Unlike tuples lists are mutable"
1584 | ]
1585 | },
1586 | {
1587 | "cell_type": "code",
1588 | "execution_count": 51,
1589 | "id": "b6be0887",
1590 | "metadata": {
1591 | "slideshow": {
1592 | "slide_type": "fragment"
1593 | }
1594 | },
1595 | "outputs": [
1596 | {
1597 | "data": {
1598 | "text/plain": [
1599 | "[0, 1, 1, 2]"
1600 | ]
1601 | },
1602 | "execution_count": 51,
1603 | "metadata": {},
1604 | "output_type": "execute_result"
1605 | }
1606 | ],
1607 | "source": [
1608 | "l"
1609 | ]
1610 | },
1611 | {
1612 | "cell_type": "code",
1613 | "execution_count": 52,
1614 | "id": "f988a477",
1615 | "metadata": {
1616 | "slideshow": {
1617 | "slide_type": "fragment"
1618 | }
1619 | },
1620 | "outputs": [
1621 | {
1622 | "ename": "TypeError",
1623 | "evalue": "'tuple' object does not support item assignment",
1624 | "output_type": "error",
1625 | "traceback": [
1626 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1627 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
1628 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;36m1\u001b[0m \u001b[0;31m# Compare to tuple\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1629 | "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment"
1630 | ]
1631 | }
1632 | ],
1633 | "source": [
1634 | "t[1] = 1 # Compare to tuple"
1635 | ]
1636 | },
1637 | {
1638 | "cell_type": "markdown",
1639 | "id": "9a54a190",
1640 | "metadata": {
1641 | "slideshow": {
1642 | "slide_type": "slide"
1643 | }
1644 | },
1645 | "source": [
1646 | "## Indexing and slicing lists"
1647 | ]
1648 | },
1649 | {
1650 | "cell_type": "code",
1651 | "execution_count": 53,
1652 | "id": "f957882d",
1653 | "metadata": {
1654 | "slideshow": {
1655 | "slide_type": "fragment"
1656 | }
1657 | },
1658 | "outputs": [
1659 | {
1660 | "data": {
1661 | "text/plain": [
1662 | "[0, 1, 1, 2]"
1663 | ]
1664 | },
1665 | "execution_count": 53,
1666 | "metadata": {},
1667 | "output_type": "execute_result"
1668 | }
1669 | ],
1670 | "source": [
1671 | "l"
1672 | ]
1673 | },
1674 | {
1675 | "cell_type": "code",
1676 | "execution_count": 54,
1677 | "id": "1099695a",
1678 | "metadata": {
1679 | "slideshow": {
1680 | "slide_type": "fragment"
1681 | }
1682 | },
1683 | "outputs": [
1684 | {
1685 | "data": {
1686 | "text/plain": [
1687 | "[1, 1, 2]"
1688 | ]
1689 | },
1690 | "execution_count": 54,
1691 | "metadata": {},
1692 | "output_type": "execute_result"
1693 | }
1694 | ],
1695 | "source": [
1696 | "l[1:] # Subset all elements starting from 2nd"
1697 | ]
1698 | },
1699 | {
1700 | "cell_type": "code",
1701 | "execution_count": 55,
1702 | "id": "c24185a6",
1703 | "metadata": {
1704 | "slideshow": {
1705 | "slide_type": "fragment"
1706 | }
1707 | },
1708 | "outputs": [
1709 | {
1710 | "data": {
1711 | "text/plain": [
1712 | "2"
1713 | ]
1714 | },
1715 | "execution_count": 55,
1716 | "metadata": {},
1717 | "output_type": "execute_result"
1718 | }
1719 | ],
1720 | "source": [
1721 | "l[-1] # Subset the last element"
1722 | ]
1723 | },
1724 | {
1725 | "cell_type": "code",
1726 | "execution_count": 56,
1727 | "id": "fa52dd22",
1728 | "metadata": {
1729 | "slideshow": {
1730 | "slide_type": "fragment"
1731 | }
1732 | },
1733 | "outputs": [
1734 | {
1735 | "data": {
1736 | "text/plain": [
1737 | "[0, 1]"
1738 | ]
1739 | },
1740 | "execution_count": 56,
1741 | "metadata": {},
1742 | "output_type": "execute_result"
1743 | }
1744 | ],
1745 | "source": [
1746 | "l[::2] # Subset every second element, list[start:stop:step]"
1747 | ]
1748 | },
1749 | {
1750 | "cell_type": "code",
1751 | "execution_count": 57,
1752 | "id": "877bb1d8",
1753 | "metadata": {
1754 | "slideshow": {
1755 | "slide_type": "fragment"
1756 | }
1757 | },
1758 | "outputs": [
1759 | {
1760 | "data": {
1761 | "text/plain": [
1762 | "[2, 1, 1, 0]"
1763 | ]
1764 | },
1765 | "execution_count": 57,
1766 | "metadata": {},
1767 | "output_type": "execute_result"
1768 | }
1769 | ],
1770 | "source": [
1771 | "l[::-1] # Subset all elements in reverse order"
1772 | ]
1773 | },
1774 | {
1775 | "cell_type": "markdown",
1776 | "id": "cc742549",
1777 | "metadata": {
1778 | "slideshow": {
1779 | "slide_type": "slide"
1780 | }
1781 | },
1782 | "source": [
1783 | "## Sets"
1784 | ]
1785 | },
1786 | {
1787 | "cell_type": "code",
1788 | "execution_count": 58,
1789 | "id": "ef31d1e2",
1790 | "metadata": {
1791 | "slideshow": {
1792 | "slide_type": "fragment"
1793 | }
1794 | },
1795 | "outputs": [
1796 | {
1797 | "data": {
1798 | "text/plain": [
1799 | "{'apple', 'banana', 'watermelon'}"
1800 | ]
1801 | },
1802 | "execution_count": 58,
1803 | "metadata": {},
1804 | "output_type": "execute_result"
1805 | }
1806 | ],
1807 | "source": [
1808 | "o"
1809 | ]
1810 | },
1811 | {
1812 | "cell_type": "code",
1813 | "execution_count": 59,
1814 | "id": "407f643a",
1815 | "metadata": {
1816 | "slideshow": {
1817 | "slide_type": "fragment"
1818 | }
1819 | },
1820 | "outputs": [
1821 | {
1822 | "data": {
1823 | "text/plain": [
1824 | "{'apple', 'banana', 'watermelon'}"
1825 | ]
1826 | },
1827 | "execution_count": 59,
1828 | "metadata": {},
1829 | "output_type": "execute_result"
1830 | }
1831 | ],
1832 | "source": [
1833 | "{'apple', 'apple', 'banana', 'watermelon'} # Sets retain only unique values"
1834 | ]
1835 | },
1836 | {
1837 | "cell_type": "code",
1838 | "execution_count": 60,
1839 | "id": "c1728fd3",
1840 | "metadata": {
1841 | "slideshow": {
1842 | "slide_type": "fragment"
1843 | }
1844 | },
1845 | "outputs": [
1846 | {
1847 | "data": {
1848 | "text/plain": [
1849 | "True"
1850 | ]
1851 | },
1852 | "execution_count": 60,
1853 | "metadata": {},
1854 | "output_type": "execute_result"
1855 | }
1856 | ],
1857 | "source": [
1858 | "{'apple'} < o # Sets can be compared (e.g. one being subset of another)"
1859 | ]
1860 | },
1861 | {
1862 | "cell_type": "code",
1863 | "execution_count": 61,
1864 | "id": "15efbfcd",
1865 | "metadata": {
1866 | "scrolled": true,
1867 | "slideshow": {
1868 | "slide_type": "fragment"
1869 | }
1870 | },
1871 | "outputs": [
1872 | {
1873 | "ename": "TypeError",
1874 | "evalue": "'set' object is not subscriptable",
1875 | "output_type": "error",
1876 | "traceback": [
1877 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
1878 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
1879 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mo\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# Unlike strings, tuples and lists, sets are unordered\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
1880 | "\u001b[0;31mTypeError\u001b[0m: 'set' object is not subscriptable"
1881 | ]
1882 | }
1883 | ],
1884 | "source": [
1885 | "o[1] # Unlike strings, tuples and lists, sets are unordered"
1886 | ]
1887 | },
1888 | {
1889 | "cell_type": "markdown",
1890 | "id": "ea56bbe5",
1891 | "metadata": {
1892 | "slideshow": {
1893 | "slide_type": "slide"
1894 | }
1895 | },
1896 | "source": [
1897 | "## Set methods in Python\n",
1898 | "\n",
1899 | "\n",
1900 | "

\n",
1901 | "
"
1902 | ]
1903 | },
1904 | {
1905 | "cell_type": "markdown",
1906 | "id": "960975e9",
1907 | "metadata": {
1908 | "slideshow": {
1909 | "slide_type": "slide"
1910 | }
1911 | },
1912 | "source": [
1913 | "## Set methods example\n",
1914 | "\n",
1915 | "\n",
1916 | "

\n",
1917 | "
\n",
1918 | "\n",
1919 | "Source: [Wikipedia](https://en.wikipedia.org/w/index.php?title=File:Supranational_European_Bodies-en.svg)"
1920 | ]
1921 | },
1922 | {
1923 | "cell_type": "markdown",
1924 | "id": "69f9b8b8",
1925 | "metadata": {
1926 | "slideshow": {
1927 | "slide_type": "slide"
1928 | }
1929 | },
1930 | "source": [
1931 | "## Set methods example continued"
1932 | ]
1933 | },
1934 | {
1935 | "cell_type": "code",
1936 | "execution_count": 62,
1937 | "id": "2faa85bb",
1938 | "metadata": {
1939 | "slideshow": {
1940 | "slide_type": "fragment"
1941 | }
1942 | },
1943 | "outputs": [],
1944 | "source": [
1945 | "nordic = {'Denmark', 'Iceland', 'Finland', 'Norway', 'Sweden'}\n",
1946 | "eu = {'Denmark', 'Finland', 'Sweden'}\n",
1947 | "krones = {'Denmark', 'Sweden'}"
1948 | ]
1949 | },
1950 | {
1951 | "cell_type": "code",
1952 | "execution_count": 63,
1953 | "id": "02bed958",
1954 | "metadata": {
1955 | "slideshow": {
1956 | "slide_type": "fragment"
1957 | }
1958 | },
1959 | "outputs": [
1960 | {
1961 | "data": {
1962 | "text/plain": [
1963 | "{'Finland'}"
1964 | ]
1965 | },
1966 | "execution_count": 63,
1967 | "metadata": {},
1968 | "output_type": "execute_result"
1969 | }
1970 | ],
1971 | "source": [
1972 | "euro = eu.difference(krones) # Same can expressed using infix operators `eu - krones`\n",
1973 | "euro"
1974 | ]
1975 | },
1976 | {
1977 | "cell_type": "code",
1978 | "execution_count": 64,
1979 | "id": "09eccecc",
1980 | "metadata": {
1981 | "slideshow": {
1982 | "slide_type": "fragment"
1983 | }
1984 | },
1985 | "outputs": [
1986 | {
1987 | "data": {
1988 | "text/plain": [
1989 | "{'Iceland', 'Liechtenstein', 'Norway', 'Switzerland'}"
1990 | ]
1991 | },
1992 | "execution_count": 64,
1993 | "metadata": {},
1994 | "output_type": "execute_result"
1995 | }
1996 | ],
1997 | "source": [
1998 | "efta = nordic.difference(eu).union({'Liechtenstein', 'Switzerland'}) # Methods calls can also be 'chained'\n",
1999 | "efta"
2000 | ]
2001 | },
2002 | {
2003 | "cell_type": "code",
2004 | "execution_count": 65,
2005 | "id": "26e5fde1",
2006 | "metadata": {
2007 | "slideshow": {
2008 | "slide_type": "fragment"
2009 | }
2010 | },
2011 | "outputs": [
2012 | {
2013 | "data": {
2014 | "text/plain": [
2015 | "{'Iceland', 'Norway'}"
2016 | ]
2017 | },
2018 | "execution_count": 65,
2019 | "metadata": {},
2020 | "output_type": "execute_result"
2021 | }
2022 | ],
2023 | "source": [
2024 | "efta.intersection(nordic) # efta & nordic"
2025 | ]
2026 | },
2027 | {
2028 | "cell_type": "code",
2029 | "execution_count": 66,
2030 | "id": "77b9d885",
2031 | "metadata": {
2032 | "slideshow": {
2033 | "slide_type": "fragment"
2034 | }
2035 | },
2036 | "outputs": [
2037 | {
2038 | "data": {
2039 | "text/plain": [
2040 | "{'Denmark',\n",
2041 | " 'Finland',\n",
2042 | " 'Iceland',\n",
2043 | " 'Liechtenstein',\n",
2044 | " 'Norway',\n",
2045 | " 'Sweden',\n",
2046 | " 'Switzerland'}"
2047 | ]
2048 | },
2049 | "execution_count": 66,
2050 | "metadata": {},
2051 | "output_type": "execute_result"
2052 | }
2053 | ],
2054 | "source": [
2055 | "schengen = efta.union(eu) # efta | eu\n",
2056 | "schengen"
2057 | ]
2058 | },
2059 | {
2060 | "cell_type": "markdown",
2061 | "id": "7e7f7289",
2062 | "metadata": {
2063 | "slideshow": {
2064 | "slide_type": "slide"
2065 | }
2066 | },
2067 | "source": [
2068 | "## Dictionaries"
2069 | ]
2070 | },
2071 | {
2072 | "cell_type": "code",
2073 | "execution_count": 67,
2074 | "id": "416ef331",
2075 | "metadata": {
2076 | "slideshow": {
2077 | "slide_type": "fragment"
2078 | }
2079 | },
2080 | "outputs": [
2081 | {
2082 | "data": {
2083 | "text/plain": [
2084 | "{'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0}"
2085 | ]
2086 | },
2087 | "execution_count": 67,
2088 | "metadata": {},
2089 | "output_type": "execute_result"
2090 | }
2091 | ],
2092 | "source": [
2093 | "d"
2094 | ]
2095 | },
2096 | {
2097 | "cell_type": "code",
2098 | "execution_count": 68,
2099 | "id": "d81cb2d2",
2100 | "metadata": {
2101 | "slideshow": {
2102 | "slide_type": "fragment"
2103 | }
2104 | },
2105 | "outputs": [
2106 | {
2107 | "data": {
2108 | "text/plain": [
2109 | "150.0"
2110 | ]
2111 | },
2112 | "execution_count": 68,
2113 | "metadata": {},
2114 | "output_type": "execute_result"
2115 | }
2116 | ],
2117 | "source": [
2118 | "d['apple'] # Unlike strings, tuples and lists, dictionaries are indexed by 'keys'"
2119 | ]
2120 | },
2121 | {
2122 | "cell_type": "code",
2123 | "execution_count": 69,
2124 | "id": "abcb55e6",
2125 | "metadata": {
2126 | "slideshow": {
2127 | "slide_type": "fragment"
2128 | }
2129 | },
2130 | "outputs": [
2131 | {
2132 | "ename": "KeyError",
2133 | "evalue": "0",
2134 | "output_type": "error",
2135 | "traceback": [
2136 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
2137 | "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
2138 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0md\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m# Rather than integers\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
2139 | "\u001b[0;31mKeyError\u001b[0m: 0"
2140 | ]
2141 | }
2142 | ],
2143 | "source": [
2144 | "d[0] # Rather than integers"
2145 | ]
2146 | },
2147 | {
2148 | "cell_type": "code",
2149 | "execution_count": 70,
2150 | "id": "b7089b59",
2151 | "metadata": {
2152 | "slideshow": {
2153 | "slide_type": "fragment"
2154 | }
2155 | },
2156 | "outputs": [
2157 | {
2158 | "data": {
2159 | "text/plain": [
2160 | "{'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0, 'strawberry': 12.0}"
2161 | ]
2162 | },
2163 | "execution_count": 70,
2164 | "metadata": {},
2165 | "output_type": "execute_result"
2166 | }
2167 | ],
2168 | "source": [
2169 | "d['strawberry'] = 12.0 # They are, however, mutable like lists and sets\n",
2170 | "d"
2171 | ]
2172 | },
2173 | {
2174 | "cell_type": "markdown",
2175 | "id": "4d0f0f1a",
2176 | "metadata": {
2177 | "slideshow": {
2178 | "slide_type": "slide"
2179 | }
2180 | },
2181 | "source": [
2182 | "## Conversion between non-scalar types"
2183 | ]
2184 | },
2185 | {
2186 | "cell_type": "code",
2187 | "execution_count": 71,
2188 | "id": "de5f6c6a",
2189 | "metadata": {
2190 | "slideshow": {
2191 | "slide_type": "fragment"
2192 | }
2193 | },
2194 | "outputs": [
2195 | {
2196 | "data": {
2197 | "text/plain": [
2198 | "(0, 'one', 1, 2)"
2199 | ]
2200 | },
2201 | "execution_count": 71,
2202 | "metadata": {},
2203 | "output_type": "execute_result"
2204 | }
2205 | ],
2206 | "source": [
2207 | "t ## Tuple"
2208 | ]
2209 | },
2210 | {
2211 | "cell_type": "code",
2212 | "execution_count": 72,
2213 | "id": "d5a6dece",
2214 | "metadata": {
2215 | "slideshow": {
2216 | "slide_type": "fragment"
2217 | }
2218 | },
2219 | "outputs": [
2220 | {
2221 | "data": {
2222 | "text/plain": [
2223 | "[0, 'one', 1, 2]"
2224 | ]
2225 | },
2226 | "execution_count": 72,
2227 | "metadata": {},
2228 | "output_type": "execute_result"
2229 | }
2230 | ],
2231 | "source": [
2232 | "list(t) ## Convert to list with a `list` function"
2233 | ]
2234 | },
2235 | {
2236 | "cell_type": "code",
2237 | "execution_count": 73,
2238 | "id": "6865444c",
2239 | "metadata": {
2240 | "slideshow": {
2241 | "slide_type": "fragment"
2242 | }
2243 | },
2244 | "outputs": [
2245 | {
2246 | "data": {
2247 | "text/plain": [
2248 | "[0, 'one', 1, 2]"
2249 | ]
2250 | },
2251 | "execution_count": 73,
2252 | "metadata": {},
2253 | "output_type": "execute_result"
2254 | }
2255 | ],
2256 | "source": [
2257 | "[x for x in t] ## List comprehesion, [expr for elem in iterable if test]"
2258 | ]
2259 | },
2260 | {
2261 | "cell_type": "code",
2262 | "execution_count": 74,
2263 | "id": "654f3bd7",
2264 | "metadata": {
2265 | "slideshow": {
2266 | "slide_type": "fragment"
2267 | }
2268 | },
2269 | "outputs": [
2270 | {
2271 | "data": {
2272 | "text/plain": [
2273 | "{0, 1, 2}"
2274 | ]
2275 | },
2276 | "execution_count": 74,
2277 | "metadata": {},
2278 | "output_type": "execute_result"
2279 | }
2280 | ],
2281 | "source": [
2282 | "set([0, 1, 1, 2]) ## Conversion to set retains only unique values"
2283 | ]
2284 | },
2285 | {
2286 | "cell_type": "markdown",
2287 | "id": "48136b87",
2288 | "metadata": {
2289 | "slideshow": {
2290 | "slide_type": "slide"
2291 | }
2292 | },
2293 | "source": [
2294 | "## None value\n",
2295 | "\n",
2296 | "- `None` is a Python null object\n",
2297 | "- It is often used to initialize objects\n",
2298 | "- And it is a return value in some functions (more on that later)"
2299 | ]
2300 | },
2301 | {
2302 | "cell_type": "code",
2303 | "execution_count": 75,
2304 | "id": "fb36fa4c",
2305 | "metadata": {
2306 | "slideshow": {
2307 | "slide_type": "fragment"
2308 | }
2309 | },
2310 | "outputs": [],
2311 | "source": [
2312 | "# Initialization of some temporary variable, which can re-assigned to another value later\n",
2313 | "tmp = None"
2314 | ]
2315 | },
2316 | {
2317 | "cell_type": "code",
2318 | "execution_count": 76,
2319 | "id": "890c5494",
2320 | "metadata": {
2321 | "slideshow": {
2322 | "slide_type": "fragment"
2323 | }
2324 | },
2325 | "outputs": [
2326 | {
2327 | "data": {
2328 | "text/plain": [
2329 | "[None, None, None, None, None, None, None, None, None, None]"
2330 | ]
2331 | },
2332 | "execution_count": 76,
2333 | "metadata": {},
2334 | "output_type": "execute_result"
2335 | }
2336 | ],
2337 | "source": [
2338 | "# Here we are initializing a list of length 10\n",
2339 | "tmp_l = [None] * 10\n",
2340 | "tmp_l"
2341 | ]
2342 | },
2343 | {
2344 | "cell_type": "code",
2345 | "execution_count": 77,
2346 | "id": "c127f281",
2347 | "metadata": {
2348 | "slideshow": {
2349 | "slide_type": "fragment"
2350 | }
2351 | },
2352 | "outputs": [
2353 | {
2354 | "data": {
2355 | "text/plain": [
2356 | "True"
2357 | ]
2358 | },
2359 | "execution_count": 77,
2360 | "metadata": {},
2361 | "output_type": "execute_result"
2362 | }
2363 | ],
2364 | "source": [
2365 | "None == None"
2366 | ]
2367 | },
2368 | {
2369 | "cell_type": "markdown",
2370 | "id": "7e5b47b5",
2371 | "metadata": {
2372 | "slideshow": {
2373 | "slide_type": "slide"
2374 | }
2375 | },
2376 | "source": [
2377 | "## Aliasing vs copying in Python\n",
2378 | "\n",
2379 | "- Assignment binds the varible name on the left of `=` sign to the object of certain type on the right.\n",
2380 | "- But the same object can have different names.\n",
2381 | "- Operations on immutable types typically overwrite the object if it gets modified.\n",
2382 | "- But for mutable objects (lists, sets, dictionaries) this can create hard-to-track problems."
2383 | ]
2384 | },
2385 | {
2386 | "cell_type": "markdown",
2387 | "id": "b68c5b93",
2388 | "metadata": {
2389 | "slideshow": {
2390 | "slide_type": "slide"
2391 | }
2392 | },
2393 | "source": [
2394 | "## Example of aliasing/copying for immutable types"
2395 | ]
2396 | },
2397 | {
2398 | "cell_type": "code",
2399 | "execution_count": 78,
2400 | "id": "9e57a4f0",
2401 | "metadata": {
2402 | "slideshow": {
2403 | "slide_type": "fragment"
2404 | }
2405 | },
2406 | "outputs": [
2407 | {
2408 | "data": {
2409 | "text/plain": [
2410 | "'test'"
2411 | ]
2412 | },
2413 | "execution_count": 78,
2414 | "metadata": {},
2415 | "output_type": "execute_result"
2416 | }
2417 | ],
2418 | "source": [
2419 | "x = 'test' # Object of type string is assinged to variable 'x'\n",
2420 | "x"
2421 | ]
2422 | },
2423 | {
2424 | "cell_type": "code",
2425 | "execution_count": 79,
2426 | "id": "c2298250",
2427 | "metadata": {
2428 | "slideshow": {
2429 | "slide_type": "fragment"
2430 | }
2431 | },
2432 | "outputs": [
2433 | {
2434 | "data": {
2435 | "text/plain": [
2436 | "'test'"
2437 | ]
2438 | },
2439 | "execution_count": 79,
2440 | "metadata": {},
2441 | "output_type": "execute_result"
2442 | }
2443 | ],
2444 | "source": [
2445 | "y = x # y is created an alias (alternative name) of x\n",
2446 | "y"
2447 | ]
2448 | },
2449 | {
2450 | "cell_type": "code",
2451 | "execution_count": 80,
2452 | "id": "6b2e6ef2",
2453 | "metadata": {
2454 | "slideshow": {
2455 | "slide_type": "fragment"
2456 | }
2457 | },
2458 | "outputs": [
2459 | {
2460 | "data": {
2461 | "text/plain": [
2462 | "'rest'"
2463 | ]
2464 | },
2465 | "execution_count": 80,
2466 | "metadata": {},
2467 | "output_type": "execute_result"
2468 | }
2469 | ],
2470 | "source": [
2471 | "x = 'rest' # Another object of type string is assigned to 'x'\n",
2472 | "x"
2473 | ]
2474 | },
2475 | {
2476 | "cell_type": "code",
2477 | "execution_count": 81,
2478 | "id": "661c9129",
2479 | "metadata": {
2480 | "slideshow": {
2481 | "slide_type": "fragment"
2482 | }
2483 | },
2484 | "outputs": [
2485 | {
2486 | "data": {
2487 | "text/plain": [
2488 | "'test'"
2489 | ]
2490 | },
2491 | "execution_count": 81,
2492 | "metadata": {},
2493 | "output_type": "execute_result"
2494 | }
2495 | ],
2496 | "source": [
2497 | "y"
2498 | ]
2499 | },
2500 | {
2501 | "cell_type": "markdown",
2502 | "id": "f3b9cdee",
2503 | "metadata": {
2504 | "slideshow": {
2505 | "slide_type": "slide"
2506 | }
2507 | },
2508 | "source": [
2509 | "## Example of aliasing/copying for mutable types"
2510 | ]
2511 | },
2512 | {
2513 | "cell_type": "code",
2514 | "execution_count": 82,
2515 | "id": "22fffbf8",
2516 | "metadata": {
2517 | "slideshow": {
2518 | "slide_type": "fragment"
2519 | }
2520 | },
2521 | "outputs": [
2522 | {
2523 | "data": {
2524 | "text/plain": [
2525 | "{'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0, 'strawberry': 12.0}"
2526 | ]
2527 | },
2528 | "execution_count": 82,
2529 | "metadata": {},
2530 | "output_type": "execute_result"
2531 | }
2532 | ],
2533 | "source": [
2534 | "d"
2535 | ]
2536 | },
2537 | {
2538 | "cell_type": "code",
2539 | "execution_count": 83,
2540 | "id": "4bd9436a",
2541 | "metadata": {
2542 | "slideshow": {
2543 | "slide_type": "fragment"
2544 | }
2545 | },
2546 | "outputs": [],
2547 | "source": [
2548 | "d1 = d # Just an alias\n",
2549 | "d2 = d.copy() # Create a copy\n",
2550 | "d['watermelon'] = 500 # Modify original dictionary"
2551 | ]
2552 | },
2553 | {
2554 | "cell_type": "code",
2555 | "execution_count": 84,
2556 | "id": "59f9a94d",
2557 | "metadata": {
2558 | "slideshow": {
2559 | "slide_type": "fragment"
2560 | }
2561 | },
2562 | "outputs": [
2563 | {
2564 | "data": {
2565 | "text/plain": [
2566 | "{'apple': 150.0, 'banana': 120.0, 'watermelon': 500, 'strawberry': 12.0}"
2567 | ]
2568 | },
2569 | "execution_count": 84,
2570 | "metadata": {},
2571 | "output_type": "execute_result"
2572 | }
2573 | ],
2574 | "source": [
2575 | "d1"
2576 | ]
2577 | },
2578 | {
2579 | "cell_type": "code",
2580 | "execution_count": 85,
2581 | "id": "e42995fc",
2582 | "metadata": {
2583 | "slideshow": {
2584 | "slide_type": "fragment"
2585 | }
2586 | },
2587 | "outputs": [
2588 | {
2589 | "data": {
2590 | "text/plain": [
2591 | "{'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0, 'strawberry': 12.0}"
2592 | ]
2593 | },
2594 | "execution_count": 85,
2595 | "metadata": {},
2596 | "output_type": "execute_result"
2597 | }
2598 | ],
2599 | "source": [
2600 | "d2"
2601 | ]
2602 | },
2603 | {
2604 | "cell_type": "markdown",
2605 | "id": "47e9c67b",
2606 | "metadata": {
2607 | "slideshow": {
2608 | "slide_type": "slide"
2609 | }
2610 | },
2611 | "source": [
2612 | "## Summary of built-in object types in Python\n",
2613 | "\n",
2614 | "| Type | Description | Scalar | Mutability | Order |\n",
2615 | "| :-----: | :-----------: | :--------: | :--------: | :-------: |\n",
2616 | "| `int` | integer | scalar | immutable | |\n",
2617 | "| `float` | real number | scalar | immutable | |\n",
2618 | "| `bool` | Boolean | scalar | immutable | |\n",
2619 | "| `None` | Python 'Null' | scalar | immutable | |\n",
2620 | "| `str` | string | non-scalar | immutable | ordered |\n",
2621 | "| `tuple` | tuple | non-scalar | immutable | ordered |\n",
2622 | "| `list` | list | non-scalar | mutable | ordered |\n",
2623 | "| `set` | set | non-scalar | mutable | unordered |\n",
2624 | "| `dict` | dictionary | non-scalar | mutable | unordered |\n",
2625 | "\n",
2626 | "[Extensive documentation on built-it types](https://docs.python.org/3/library/stdtypes.html)"
2627 | ]
2628 | },
2629 | {
2630 | "cell_type": "markdown",
2631 | "id": "6aa8b5c7",
2632 | "metadata": {
2633 | "slideshow": {
2634 | "slide_type": "slide"
2635 | }
2636 | },
2637 | "source": [
2638 | "## Modules\n",
2639 | "\n",
2640 | "- Python's power lies in its extensibility\n",
2641 | "- This is usually achieved by loading additional modules (libraries)\n",
2642 | "- Module can be just a `.py` file that you import into your program (script)\n",
2643 | "- However, often this refers to external libraries installed using `pip` or `conda`\n",
2644 | "- Standard Python installation also includes a number of modules (full list [here](https://docs.python.org/3/library/index.html))"
2645 | ]
2646 | },
2647 | {
2648 | "cell_type": "markdown",
2649 | "id": "f703bf36",
2650 | "metadata": {
2651 | "slideshow": {
2652 | "slide_type": "slide"
2653 | }
2654 | },
2655 | "source": [
2656 | "## Basic statistical operations"
2657 | ]
2658 | },
2659 | {
2660 | "cell_type": "code",
2661 | "execution_count": 86,
2662 | "id": "e13012d4",
2663 | "metadata": {
2664 | "slideshow": {
2665 | "slide_type": "fragment"
2666 | }
2667 | },
2668 | "outputs": [],
2669 | "source": [
2670 | "import statistics # Standard Python module\n",
2671 | "fib = [0, 1, 1, 2, 3, 5]"
2672 | ]
2673 | },
2674 | {
2675 | "cell_type": "code",
2676 | "execution_count": 87,
2677 | "id": "b2dbad22",
2678 | "metadata": {
2679 | "slideshow": {
2680 | "slide_type": "fragment"
2681 | }
2682 | },
2683 | "outputs": [
2684 | {
2685 | "data": {
2686 | "text/plain": [
2687 | "2"
2688 | ]
2689 | },
2690 | "execution_count": 87,
2691 | "metadata": {},
2692 | "output_type": "execute_result"
2693 | }
2694 | ],
2695 | "source": [
2696 | "statistics.mean(fib) # Mean"
2697 | ]
2698 | },
2699 | {
2700 | "cell_type": "code",
2701 | "execution_count": 88,
2702 | "id": "1b6aeb37",
2703 | "metadata": {
2704 | "slideshow": {
2705 | "slide_type": "fragment"
2706 | }
2707 | },
2708 | "outputs": [
2709 | {
2710 | "data": {
2711 | "text/plain": [
2712 | "1.5"
2713 | ]
2714 | },
2715 | "execution_count": 88,
2716 | "metadata": {},
2717 | "output_type": "execute_result"
2718 | }
2719 | ],
2720 | "source": [
2721 | "statistics.median(fib) # Median"
2722 | ]
2723 | },
2724 | {
2725 | "cell_type": "code",
2726 | "execution_count": 89,
2727 | "id": "92fab746",
2728 | "metadata": {
2729 | "slideshow": {
2730 | "slide_type": "fragment"
2731 | }
2732 | },
2733 | "outputs": [
2734 | {
2735 | "data": {
2736 | "text/plain": [
2737 | "1"
2738 | ]
2739 | },
2740 | "execution_count": 89,
2741 | "metadata": {},
2742 | "output_type": "execute_result"
2743 | }
2744 | ],
2745 | "source": [
2746 | "statistics.mode(fib) # Mode"
2747 | ]
2748 | },
2749 | {
2750 | "cell_type": "code",
2751 | "execution_count": 90,
2752 | "id": "792eba7e",
2753 | "metadata": {
2754 | "slideshow": {
2755 | "slide_type": "fragment"
2756 | }
2757 | },
2758 | "outputs": [
2759 | {
2760 | "data": {
2761 | "text/plain": [
2762 | "1.7888543819998317"
2763 | ]
2764 | },
2765 | "execution_count": 90,
2766 | "metadata": {},
2767 | "output_type": "execute_result"
2768 | }
2769 | ],
2770 | "source": [
2771 | "statistics.stdev(fib) # Standard deviation"
2772 | ]
2773 | },
2774 | {
2775 | "cell_type": "markdown",
2776 | "id": "dd933874",
2777 | "metadata": {
2778 | "slideshow": {
2779 | "slide_type": "slide"
2780 | }
2781 | },
2782 | "source": [
2783 | "## Help!\n",
2784 | "\n",
2785 | "Python has an inbuilt help facility which provides more information about any object:"
2786 | ]
2787 | },
2788 | {
2789 | "cell_type": "code",
2790 | "execution_count": 91,
2791 | "id": "8c4d969d",
2792 | "metadata": {
2793 | "slideshow": {
2794 | "slide_type": "fragment"
2795 | }
2796 | },
2797 | "outputs": [],
2798 | "source": [
2799 | "?s"
2800 | ]
2801 | },
2802 | {
2803 | "cell_type": "code",
2804 | "execution_count": 92,
2805 | "id": "520bc3c5",
2806 | "metadata": {
2807 | "slideshow": {
2808 | "slide_type": "fragment"
2809 | }
2810 | },
2811 | "outputs": [
2812 | {
2813 | "name": "stdout",
2814 | "output_type": "stream",
2815 | "text": [
2816 | "Help on built-in function join:\n",
2817 | "\n",
2818 | "join(iterable, /) method of builtins.str instance\n",
2819 | " Concatenate any number of strings.\n",
2820 | " \n",
2821 | " The string whose method is called is inserted in between each given string.\n",
2822 | " The result is returned as a new string.\n",
2823 | " \n",
2824 | " Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n",
2825 | "\n"
2826 | ]
2827 | }
2828 | ],
2829 | "source": [
2830 | "help(s.join)"
2831 | ]
2832 | },
2833 | {
2834 | "cell_type": "markdown",
2835 | "id": "5c19699b",
2836 | "metadata": {
2837 | "slideshow": {
2838 | "slide_type": "fragment"
2839 | }
2840 | },
2841 | "source": [
2842 | "- The quality of the documentation varies hugely across libraries\n",
2843 | "- [Stackoverflow](https://stackoverflow.com/) is a good resource for many standard tasks\n",
2844 | "- For custom packages it is often helpful to check the **issues** page on the [GitHub](https://github.com/)\n",
2845 | "- E.g. for `pandas`: [https://github.com/pandas-dev/pandas/issues](https://github.com/pandas-dev/pandas/issues)\n",
2846 | "- Or, indeed, any search engine [#LMDDGTFY](https://lmddgtfy.net/)"
2847 | ]
2848 | },
2849 | {
2850 | "cell_type": "markdown",
2851 | "id": "cdf4b9ff",
2852 | "metadata": {
2853 | "slideshow": {
2854 | "slide_type": "slide"
2855 | }
2856 | },
2857 | "source": [
2858 | "## Next\n",
2859 | "\n",
2860 | "- Pandas\n",
2861 | "- Data I/O\n"
2862 | ]
2863 | }
2864 | ],
2865 | "metadata": {
2866 | "celltoolbar": "Slideshow",
2867 | "kernelspec": {
2868 | "display_name": "Python 3",
2869 | "language": "python",
2870 | "name": "python3"
2871 | },
2872 | "language_info": {
2873 | "codemirror_mode": {
2874 | "name": "ipython",
2875 | "version": 3
2876 | },
2877 | "file_extension": ".py",
2878 | "mimetype": "text/x-python",
2879 | "name": "python",
2880 | "nbconvert_exporter": "python",
2881 | "pygments_lexer": "ipython3",
2882 | "version": "3.8.10"
2883 | }
2884 | },
2885 | "nbformat": 4,
2886 | "nbformat_minor": 5
2887 | }
2888 |
--------------------------------------------------------------------------------
/lectures/03_pandas.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "8f6c8dc2",
6 | "metadata": {
7 | "slideshow": {
8 | "slide_type": "slide"
9 | }
10 | },
11 | "source": [
12 | "# Part 3: Pandas and Data I/O\n",
13 | "\n",
14 | "## Introduction to Python\n",
15 | "\n",
16 | "### Tom Paskhalis\n",
17 | "\n",
18 | "##### 2022-07-27\n",
19 | "\n",
20 | "##### Data Science Summer School 2022"
21 | ]
22 | },
23 | {
24 | "cell_type": "markdown",
25 | "id": "92e6b394",
26 | "metadata": {
27 | "slideshow": {
28 | "slide_type": "slide"
29 | }
30 | },
31 | "source": [
32 | "## Rectangular data\n",
33 | "\n",
34 | "\n",
35 | "

\n",
36 | "
\n",
37 | "\n",
38 | "History of rectangular data goes back to punchcards with origins in US census data processing.\n",
39 | "\n",
40 | "Source: [Wikipedia](https://en.wikipedia.org/wiki/Punched_card) "
41 | ]
42 | },
43 | {
44 | "cell_type": "markdown",
45 | "id": "6e61f983",
46 | "metadata": {
47 | "slideshow": {
48 | "slide_type": "slide"
49 | }
50 | },
51 | "source": [
52 | "## Tidy data\n",
53 | "\n",
54 | "- Tidy data is a specific subset of rectangular data, where:\n",
55 | " - Each variable is in a column\n",
56 | " - Each observation is in a row\n",
57 | " - Each value is in a cell\n",
58 | "\n",
59 | "\n",
60 | "

\n",
61 | "
\n",
62 | "\n",
63 | "Source: [R for Data Science](https://r4ds.had.co.nz/tidy-data.html) "
64 | ]
65 | },
66 | {
67 | "cell_type": "markdown",
68 | "id": "0df582d4",
69 | "metadata": {
70 | "slideshow": {
71 | "slide_type": "slide"
72 | }
73 | },
74 | "source": [
75 | "## Data in Python\n",
76 | "\n",
77 | "- Python can hold and manipulate > 1 dataset at the same time\n",
78 | "- Python stores objects in memory\n",
79 | "- The limit on the size of data is determined by your computer memory\n",
80 | "- Most functionality for dealing with data is provided by external libraries"
81 | ]
82 | },
83 | {
84 | "cell_type": "markdown",
85 | "id": "7e5b1eb7",
86 | "metadata": {
87 | "slideshow": {
88 | "slide_type": "slide"
89 | }
90 | },
91 | "source": [
92 | "## Pandas\n",
93 | "\n",
94 | "- Standard Python library does not have data type for tabular data\n",
95 | "- However, `pandas` library has become the de facto standard for data manipulation\n",
96 | "- pandas is built upon (and often used in conjuction with) other computational libraries\n",
97 | "- E.g. `numpy` (array data type), `scipy` (linear algebra) and `scikit-learn` (machine learning)"
98 | ]
99 | },
100 | {
101 | "cell_type": "code",
102 | "execution_count": 1,
103 | "id": "b1d323c4",
104 | "metadata": {
105 | "slideshow": {
106 | "slide_type": "fragment"
107 | }
108 | },
109 | "outputs": [],
110 | "source": [
111 | "# Using 'as' allows to avoid typing full name each time the module is referred to\n",
112 | "import pandas as pd"
113 | ]
114 | },
115 | {
116 | "cell_type": "markdown",
117 | "id": "b7985032",
118 | "metadata": {
119 | "slideshow": {
120 | "slide_type": "slide"
121 | }
122 | },
123 | "source": [
124 | "## Series\n",
125 | "\n",
126 | "- *Series* is a one-dimensional array-like object"
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": 2,
132 | "id": "36416a27",
133 | "metadata": {
134 | "slideshow": {
135 | "slide_type": "fragment"
136 | }
137 | },
138 | "outputs": [
139 | {
140 | "data": {
141 | "text/plain": [
142 | "0 150.0\n",
143 | "1 120.0\n",
144 | "2 3000.0\n",
145 | "dtype: float64"
146 | ]
147 | },
148 | "execution_count": 2,
149 | "metadata": {},
150 | "output_type": "execute_result"
151 | }
152 | ],
153 | "source": [
154 | "sr1 = pd.Series([150.0, 120.0, 3000.0])\n",
155 | "sr1"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 3,
161 | "id": "a5b81214",
162 | "metadata": {
163 | "slideshow": {
164 | "slide_type": "fragment"
165 | }
166 | },
167 | "outputs": [
168 | {
169 | "data": {
170 | "text/plain": [
171 | "150.0"
172 | ]
173 | },
174 | "execution_count": 3,
175 | "metadata": {},
176 | "output_type": "execute_result"
177 | }
178 | ],
179 | "source": [
180 | "sr1[0] # Slicing is simiar to standard Python objects"
181 | ]
182 | },
183 | {
184 | "cell_type": "code",
185 | "execution_count": 4,
186 | "id": "2f154249",
187 | "metadata": {
188 | "slideshow": {
189 | "slide_type": "fragment"
190 | }
191 | },
192 | "outputs": [
193 | {
194 | "data": {
195 | "text/plain": [
196 | "2 3000.0\n",
197 | "dtype: float64"
198 | ]
199 | },
200 | "execution_count": 4,
201 | "metadata": {},
202 | "output_type": "execute_result"
203 | }
204 | ],
205 | "source": [
206 | "sr1[sr1 > 200]"
207 | ]
208 | },
209 | {
210 | "cell_type": "markdown",
211 | "id": "eed8bfde",
212 | "metadata": {
213 | "slideshow": {
214 | "slide_type": "slide"
215 | }
216 | },
217 | "source": [
218 | "## Indexing in Series\n",
219 | "\n",
220 | "- Another way to think about Series is as a ordered dictionary"
221 | ]
222 | },
223 | {
224 | "cell_type": "code",
225 | "execution_count": 5,
226 | "id": "f55281b1",
227 | "metadata": {
228 | "slideshow": {
229 | "slide_type": "fragment"
230 | }
231 | },
232 | "outputs": [],
233 | "source": [
234 | "d = {'apple': 150.0, 'banana': 120.0, 'watermelon': 3000.0}"
235 | ]
236 | },
237 | {
238 | "cell_type": "code",
239 | "execution_count": 6,
240 | "id": "ef327351",
241 | "metadata": {
242 | "slideshow": {
243 | "slide_type": "fragment"
244 | }
245 | },
246 | "outputs": [
247 | {
248 | "data": {
249 | "text/plain": [
250 | "apple 150.0\n",
251 | "banana 120.0\n",
252 | "watermelon 3000.0\n",
253 | "dtype: float64"
254 | ]
255 | },
256 | "execution_count": 6,
257 | "metadata": {},
258 | "output_type": "execute_result"
259 | }
260 | ],
261 | "source": [
262 | "sr2 = pd.Series(d)\n",
263 | "sr2"
264 | ]
265 | },
266 | {
267 | "cell_type": "code",
268 | "execution_count": 7,
269 | "id": "08716e45",
270 | "metadata": {
271 | "slideshow": {
272 | "slide_type": "fragment"
273 | }
274 | },
275 | "outputs": [
276 | {
277 | "data": {
278 | "text/plain": [
279 | "150.0"
280 | ]
281 | },
282 | "execution_count": 7,
283 | "metadata": {},
284 | "output_type": "execute_result"
285 | }
286 | ],
287 | "source": [
288 | "sr2[0] # Recall that this slicing would be impossible for standard dictionary"
289 | ]
290 | },
291 | {
292 | "cell_type": "code",
293 | "execution_count": 8,
294 | "id": "cbe5b4b4",
295 | "metadata": {
296 | "slideshow": {
297 | "slide_type": "fragment"
298 | }
299 | },
300 | "outputs": [
301 | {
302 | "data": {
303 | "text/plain": [
304 | "Index(['apple', 'banana', 'watermelon'], dtype='object')"
305 | ]
306 | },
307 | "execution_count": 8,
308 | "metadata": {},
309 | "output_type": "execute_result"
310 | }
311 | ],
312 | "source": [
313 | "sr2.index"
314 | ]
315 | },
316 | {
317 | "cell_type": "markdown",
318 | "id": "dbcce734",
319 | "metadata": {
320 | "slideshow": {
321 | "slide_type": "slide"
322 | }
323 | },
324 | "source": [
325 | "## DataFrame - the workhorse of data analysis\n",
326 | "\n",
327 | "- *DataFrame* is a rectangular table of data"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 9,
333 | "id": "981a5621",
334 | "metadata": {
335 | "slideshow": {
336 | "slide_type": "fragment"
337 | }
338 | },
339 | "outputs": [
340 | {
341 | "data": {
342 | "text/html": [
343 | "\n",
344 | "\n",
357 | "
\n",
358 | " \n",
359 | " \n",
360 | " | \n",
361 | " fruit | \n",
362 | " weight | \n",
363 | " berry | \n",
364 | "
\n",
365 | " \n",
366 | " \n",
367 | " \n",
368 | " 0 | \n",
369 | " apple | \n",
370 | " 150.0 | \n",
371 | " False | \n",
372 | "
\n",
373 | " \n",
374 | " 1 | \n",
375 | " banana | \n",
376 | " 120.0 | \n",
377 | " True | \n",
378 | "
\n",
379 | " \n",
380 | " 2 | \n",
381 | " watermelon | \n",
382 | " 3000.0 | \n",
383 | " True | \n",
384 | "
\n",
385 | " \n",
386 | "
\n",
387 | "
"
388 | ],
389 | "text/plain": [
390 | " fruit weight berry\n",
391 | "0 apple 150.0 False\n",
392 | "1 banana 120.0 True\n",
393 | "2 watermelon 3000.0 True"
394 | ]
395 | },
396 | "execution_count": 9,
397 | "metadata": {},
398 | "output_type": "execute_result"
399 | }
400 | ],
401 | "source": [
402 | "data = {'fruit': ['apple', 'banana', 'watermelon'], # DataFrame can be constructed from\n",
403 | " 'weight': [150.0, 120.0, 3000.0], # a dict of equal-length lists/arrays\n",
404 | " 'berry': [False, True, True]} \n",
405 | "df = pd.DataFrame(data)\n",
406 | "df"
407 | ]
408 | },
409 | {
410 | "cell_type": "markdown",
411 | "id": "b1ed6ca8",
412 | "metadata": {
413 | "slideshow": {
414 | "slide_type": "slide"
415 | }
416 | },
417 | "source": [
418 | "## Indexing in DataFrame\n",
419 | "\n",
420 | "- DataFrame has both row and column indices\n",
421 | "- `DataFrame.loc()` provides method for *label* location\n",
422 | "- `DataFrame.iloc()` provides method for *index* location"
423 | ]
424 | },
425 | {
426 | "cell_type": "code",
427 | "execution_count": 10,
428 | "id": "04c93d38",
429 | "metadata": {
430 | "slideshow": {
431 | "slide_type": "fragment"
432 | }
433 | },
434 | "outputs": [
435 | {
436 | "data": {
437 | "text/plain": [
438 | "fruit apple\n",
439 | "weight 150.0\n",
440 | "berry False\n",
441 | "Name: 0, dtype: object"
442 | ]
443 | },
444 | "execution_count": 10,
445 | "metadata": {},
446 | "output_type": "execute_result"
447 | }
448 | ],
449 | "source": [
450 | "df.iloc[0] # First row"
451 | ]
452 | },
453 | {
454 | "cell_type": "code",
455 | "execution_count": 11,
456 | "id": "b271a5c7",
457 | "metadata": {
458 | "slideshow": {
459 | "slide_type": "fragment"
460 | }
461 | },
462 | "outputs": [
463 | {
464 | "data": {
465 | "text/plain": [
466 | "0 apple\n",
467 | "1 banana\n",
468 | "2 watermelon\n",
469 | "Name: fruit, dtype: object"
470 | ]
471 | },
472 | "execution_count": 11,
473 | "metadata": {},
474 | "output_type": "execute_result"
475 | }
476 | ],
477 | "source": [
478 | "df.iloc[:,0] # First column"
479 | ]
480 | },
481 | {
482 | "cell_type": "markdown",
483 | "id": "22b83a1c",
484 | "metadata": {
485 | "slideshow": {
486 | "slide_type": "slide"
487 | }
488 | },
489 | "source": [
490 | "## Summary of indexing in DataFrame\n",
491 | "\n",
492 | "| Expression | Selection Operation |\n",
493 | "|:-----------------------|:--------------------------------------------------------|\n",
494 | "| `df[val]` | Column or sequence of columns +convenience (e.g. slice) |\n",
495 | "| `df.loc[lab_i]` | Row or subset of rows by label |\n",
496 | "| `df.loc[:, lab_j]` | Column or subset of columns by label |\n",
497 | "| `df.loc[lab_i, lab_j]` | Both rows and columns by label |\n",
498 | "| `df.iloc[i]` | Row or subset of rows by integer position |\n",
499 | "| `df.iloc[:, j]` | Column or subset of columns by integer position |\n",
500 | "| `df.iloc[i, j]` | Both rows and columns by integer position |\n",
501 | "| `df.at[lab_i, lab_j]` | Single scalar value by row and column label |\n",
502 | "| `df.iat[i, j]` | Single scalar value by row and column integer position |\n",
503 | "\n",
504 | "Extra: [Pandas documentation on indexing](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html)"
505 | ]
506 | },
507 | {
508 | "cell_type": "markdown",
509 | "id": "f90c99d3",
510 | "metadata": {
511 | "slideshow": {
512 | "slide_type": "slide"
513 | }
514 | },
515 | "source": [
516 | "## Subsetting in DataFrame"
517 | ]
518 | },
519 | {
520 | "cell_type": "code",
521 | "execution_count": 12,
522 | "id": "79008bf8",
523 | "metadata": {
524 | "slideshow": {
525 | "slide_type": "fragment"
526 | }
527 | },
528 | "outputs": [
529 | {
530 | "data": {
531 | "text/html": [
532 | "\n",
533 | "\n",
546 | "
\n",
547 | " \n",
548 | " \n",
549 | " | \n",
550 | " fruit | \n",
551 | " weight | \n",
552 | " berry | \n",
553 | "
\n",
554 | " \n",
555 | " \n",
556 | " \n",
557 | " 0 | \n",
558 | " apple | \n",
559 | " 150.0 | \n",
560 | " False | \n",
561 | "
\n",
562 | " \n",
563 | " 1 | \n",
564 | " banana | \n",
565 | " 120.0 | \n",
566 | " True | \n",
567 | "
\n",
568 | " \n",
569 | "
\n",
570 | "
"
571 | ],
572 | "text/plain": [
573 | " fruit weight berry\n",
574 | "0 apple 150.0 False\n",
575 | "1 banana 120.0 True"
576 | ]
577 | },
578 | "execution_count": 12,
579 | "metadata": {},
580 | "output_type": "execute_result"
581 | }
582 | ],
583 | "source": [
584 | "df.iloc[:2] # Select the first two rows (with convenience shortcut for slicing)"
585 | ]
586 | },
587 | {
588 | "cell_type": "code",
589 | "execution_count": 13,
590 | "id": "52d67759",
591 | "metadata": {
592 | "slideshow": {
593 | "slide_type": "fragment"
594 | }
595 | },
596 | "outputs": [
597 | {
598 | "data": {
599 | "text/html": [
600 | "\n",
601 | "\n",
614 | "
\n",
615 | " \n",
616 | " \n",
617 | " | \n",
618 | " fruit | \n",
619 | " weight | \n",
620 | " berry | \n",
621 | "
\n",
622 | " \n",
623 | " \n",
624 | " \n",
625 | " 0 | \n",
626 | " apple | \n",
627 | " 150.0 | \n",
628 | " False | \n",
629 | "
\n",
630 | " \n",
631 | " 1 | \n",
632 | " banana | \n",
633 | " 120.0 | \n",
634 | " True | \n",
635 | "
\n",
636 | " \n",
637 | "
\n",
638 | "
"
639 | ],
640 | "text/plain": [
641 | " fruit weight berry\n",
642 | "0 apple 150.0 False\n",
643 | "1 banana 120.0 True"
644 | ]
645 | },
646 | "execution_count": 13,
647 | "metadata": {},
648 | "output_type": "execute_result"
649 | }
650 | ],
651 | "source": [
652 | "df[:2] # Shortcut"
653 | ]
654 | },
655 | {
656 | "cell_type": "code",
657 | "execution_count": 14,
658 | "id": "f50778bd",
659 | "metadata": {
660 | "slideshow": {
661 | "slide_type": "fragment"
662 | }
663 | },
664 | "outputs": [
665 | {
666 | "data": {
667 | "text/html": [
668 | "\n",
669 | "\n",
682 | "
\n",
683 | " \n",
684 | " \n",
685 | " | \n",
686 | " fruit | \n",
687 | " berry | \n",
688 | "
\n",
689 | " \n",
690 | " \n",
691 | " \n",
692 | " 0 | \n",
693 | " apple | \n",
694 | " False | \n",
695 | "
\n",
696 | " \n",
697 | " 1 | \n",
698 | " banana | \n",
699 | " True | \n",
700 | "
\n",
701 | " \n",
702 | " 2 | \n",
703 | " watermelon | \n",
704 | " True | \n",
705 | "
\n",
706 | " \n",
707 | "
\n",
708 | "
"
709 | ],
710 | "text/plain": [
711 | " fruit berry\n",
712 | "0 apple False\n",
713 | "1 banana True\n",
714 | "2 watermelon True"
715 | ]
716 | },
717 | "execution_count": 14,
718 | "metadata": {},
719 | "output_type": "execute_result"
720 | }
721 | ],
722 | "source": [
723 | "df.loc[:, ['fruit', 'berry']] # Select the columns 'fruit' and 'berry'"
724 | ]
725 | },
726 | {
727 | "cell_type": "code",
728 | "execution_count": 15,
729 | "id": "83a7166c",
730 | "metadata": {
731 | "slideshow": {
732 | "slide_type": "fragment"
733 | }
734 | },
735 | "outputs": [
736 | {
737 | "data": {
738 | "text/html": [
739 | "\n",
740 | "\n",
753 | "
\n",
754 | " \n",
755 | " \n",
756 | " | \n",
757 | " fruit | \n",
758 | " berry | \n",
759 | "
\n",
760 | " \n",
761 | " \n",
762 | " \n",
763 | " 0 | \n",
764 | " apple | \n",
765 | " False | \n",
766 | "
\n",
767 | " \n",
768 | " 1 | \n",
769 | " banana | \n",
770 | " True | \n",
771 | "
\n",
772 | " \n",
773 | " 2 | \n",
774 | " watermelon | \n",
775 | " True | \n",
776 | "
\n",
777 | " \n",
778 | "
\n",
779 | "
"
780 | ],
781 | "text/plain": [
782 | " fruit berry\n",
783 | "0 apple False\n",
784 | "1 banana True\n",
785 | "2 watermelon True"
786 | ]
787 | },
788 | "execution_count": 15,
789 | "metadata": {},
790 | "output_type": "execute_result"
791 | }
792 | ],
793 | "source": [
794 | "df[['fruit', 'berry']] # Shortcut"
795 | ]
796 | },
797 | {
798 | "cell_type": "markdown",
799 | "id": "4e02d724",
800 | "metadata": {
801 | "slideshow": {
802 | "slide_type": "slide"
803 | }
804 | },
805 | "source": [
806 | "## Columns in DataFrame"
807 | ]
808 | },
809 | {
810 | "cell_type": "code",
811 | "execution_count": 16,
812 | "id": "ab92435a",
813 | "metadata": {
814 | "slideshow": {
815 | "slide_type": "fragment"
816 | }
817 | },
818 | "outputs": [
819 | {
820 | "data": {
821 | "text/plain": [
822 | "Index(['fruit', 'weight', 'berry'], dtype='object')"
823 | ]
824 | },
825 | "execution_count": 16,
826 | "metadata": {},
827 | "output_type": "execute_result"
828 | }
829 | ],
830 | "source": [
831 | "df.columns # Retrieve the names of all columns"
832 | ]
833 | },
834 | {
835 | "cell_type": "code",
836 | "execution_count": 17,
837 | "id": "6a4ee129",
838 | "metadata": {
839 | "slideshow": {
840 | "slide_type": "fragment"
841 | }
842 | },
843 | "outputs": [
844 | {
845 | "data": {
846 | "text/plain": [
847 | "'fruit'"
848 | ]
849 | },
850 | "execution_count": 17,
851 | "metadata": {},
852 | "output_type": "execute_result"
853 | }
854 | ],
855 | "source": [
856 | "df.columns[0] # This Index object is subsettable"
857 | ]
858 | },
859 | {
860 | "cell_type": "code",
861 | "execution_count": 18,
862 | "id": "430d8786",
863 | "metadata": {
864 | "slideshow": {
865 | "slide_type": "fragment"
866 | }
867 | },
868 | "outputs": [
869 | {
870 | "data": {
871 | "text/plain": [
872 | "array([ True, False, False])"
873 | ]
874 | },
875 | "execution_count": 18,
876 | "metadata": {},
877 | "output_type": "execute_result"
878 | }
879 | ],
880 | "source": [
881 | "df.columns.str.startswith('fr') # As column names are strings, we can apply str methods"
882 | ]
883 | },
884 | {
885 | "cell_type": "code",
886 | "execution_count": 19,
887 | "id": "73a7f8a5",
888 | "metadata": {
889 | "slideshow": {
890 | "slide_type": "fragment"
891 | }
892 | },
893 | "outputs": [
894 | {
895 | "data": {
896 | "text/html": [
897 | "\n",
898 | "\n",
911 | "
\n",
912 | " \n",
913 | " \n",
914 | " | \n",
915 | " fruit | \n",
916 | "
\n",
917 | " \n",
918 | " \n",
919 | " \n",
920 | " 0 | \n",
921 | " apple | \n",
922 | "
\n",
923 | " \n",
924 | " 1 | \n",
925 | " banana | \n",
926 | "
\n",
927 | " \n",
928 | " 2 | \n",
929 | " watermelon | \n",
930 | "
\n",
931 | " \n",
932 | "
\n",
933 | "
"
934 | ],
935 | "text/plain": [
936 | " fruit\n",
937 | "0 apple\n",
938 | "1 banana\n",
939 | "2 watermelon"
940 | ]
941 | },
942 | "execution_count": 19,
943 | "metadata": {},
944 | "output_type": "execute_result"
945 | }
946 | ],
947 | "source": [
948 | "df.iloc[:,df.columns.str.startswith('fr')] # This is helpful with more complicated column selection criteria"
949 | ]
950 | },
951 | {
952 | "cell_type": "markdown",
953 | "id": "37f26c55",
954 | "metadata": {
955 | "slideshow": {
956 | "slide_type": "slide"
957 | }
958 | },
959 | "source": [
960 | "## Filtering DataFrame"
961 | ]
962 | },
963 | {
964 | "cell_type": "code",
965 | "execution_count": 20,
966 | "id": "2b670c07",
967 | "metadata": {
968 | "slideshow": {
969 | "slide_type": "fragment"
970 | }
971 | },
972 | "outputs": [
973 | {
974 | "data": {
975 | "text/html": [
976 | "\n",
977 | "\n",
990 | "
\n",
991 | " \n",
992 | " \n",
993 | " | \n",
994 | " fruit | \n",
995 | " weight | \n",
996 | " berry | \n",
997 | "
\n",
998 | " \n",
999 | " \n",
1000 | " \n",
1001 | " 0 | \n",
1002 | " apple | \n",
1003 | " 150.0 | \n",
1004 | " False | \n",
1005 | "
\n",
1006 | " \n",
1007 | "
\n",
1008 | "
"
1009 | ],
1010 | "text/plain": [
1011 | " fruit weight berry\n",
1012 | "0 apple 150.0 False"
1013 | ]
1014 | },
1015 | "execution_count": 20,
1016 | "metadata": {},
1017 | "output_type": "execute_result"
1018 | }
1019 | ],
1020 | "source": [
1021 | "df[df.loc[:,'berry'] == False] # Select rows where fruits are not berries"
1022 | ]
1023 | },
1024 | {
1025 | "cell_type": "code",
1026 | "execution_count": 21,
1027 | "id": "8b2388ae",
1028 | "metadata": {
1029 | "slideshow": {
1030 | "slide_type": "fragment"
1031 | }
1032 | },
1033 | "outputs": [
1034 | {
1035 | "data": {
1036 | "text/html": [
1037 | "\n",
1038 | "\n",
1051 | "
\n",
1052 | " \n",
1053 | " \n",
1054 | " | \n",
1055 | " fruit | \n",
1056 | " weight | \n",
1057 | " berry | \n",
1058 | "
\n",
1059 | " \n",
1060 | " \n",
1061 | " \n",
1062 | " 0 | \n",
1063 | " apple | \n",
1064 | " 150.0 | \n",
1065 | " False | \n",
1066 | "
\n",
1067 | " \n",
1068 | "
\n",
1069 | "
"
1070 | ],
1071 | "text/plain": [
1072 | " fruit weight berry\n",
1073 | "0 apple 150.0 False"
1074 | ]
1075 | },
1076 | "execution_count": 21,
1077 | "metadata": {},
1078 | "output_type": "execute_result"
1079 | }
1080 | ],
1081 | "source": [
1082 | "df[df['berry'] == False] # The same can be achieved with more concise syntax"
1083 | ]
1084 | },
1085 | {
1086 | "cell_type": "code",
1087 | "execution_count": 22,
1088 | "id": "4ce8c635",
1089 | "metadata": {
1090 | "slideshow": {
1091 | "slide_type": "fragment"
1092 | }
1093 | },
1094 | "outputs": [
1095 | {
1096 | "data": {
1097 | "text/html": [
1098 | "\n",
1099 | "\n",
1112 | "
\n",
1113 | " \n",
1114 | " \n",
1115 | " | \n",
1116 | " fruit | \n",
1117 | " weight | \n",
1118 | " berry | \n",
1119 | "
\n",
1120 | " \n",
1121 | " \n",
1122 | " \n",
1123 | " 2 | \n",
1124 | " watermelon | \n",
1125 | " 3000.0 | \n",
1126 | " True | \n",
1127 | "
\n",
1128 | " \n",
1129 | "
\n",
1130 | "
"
1131 | ],
1132 | "text/plain": [
1133 | " fruit weight berry\n",
1134 | "2 watermelon 3000.0 True"
1135 | ]
1136 | },
1137 | "execution_count": 22,
1138 | "metadata": {},
1139 | "output_type": "execute_result"
1140 | }
1141 | ],
1142 | "source": [
1143 | "weight200 = df[df['weight'] > 200] # Create new dataset with rows where weight is higher than 200\n",
1144 | "weight200"
1145 | ]
1146 | },
1147 | {
1148 | "cell_type": "markdown",
1149 | "id": "0dc228ee",
1150 | "metadata": {
1151 | "slideshow": {
1152 | "slide_type": "slide"
1153 | }
1154 | },
1155 | "source": [
1156 | "## Variable transformation\n",
1157 | "\n",
1158 | "- Lambda functions can be used to transform data with `map()` method"
1159 | ]
1160 | },
1161 | {
1162 | "cell_type": "code",
1163 | "execution_count": 23,
1164 | "id": "0408a40d",
1165 | "metadata": {
1166 | "slideshow": {
1167 | "slide_type": "fragment"
1168 | }
1169 | },
1170 | "outputs": [
1171 | {
1172 | "data": {
1173 | "text/plain": [
1174 | "0 APPLE\n",
1175 | "1 BANANA\n",
1176 | "2 WATERMELON\n",
1177 | "Name: fruit, dtype: object"
1178 | ]
1179 | },
1180 | "execution_count": 23,
1181 | "metadata": {},
1182 | "output_type": "execute_result"
1183 | }
1184 | ],
1185 | "source": [
1186 | "df['fruit'].map(lambda x: x.upper())"
1187 | ]
1188 | },
1189 | {
1190 | "cell_type": "code",
1191 | "execution_count": 24,
1192 | "id": "20f4d4a1",
1193 | "metadata": {
1194 | "slideshow": {
1195 | "slide_type": "fragment"
1196 | }
1197 | },
1198 | "outputs": [],
1199 | "source": [
1200 | "transform = lambda x: x.capitalize()"
1201 | ]
1202 | },
1203 | {
1204 | "cell_type": "code",
1205 | "execution_count": 25,
1206 | "id": "5becfe3e",
1207 | "metadata": {
1208 | "slideshow": {
1209 | "slide_type": "fragment"
1210 | }
1211 | },
1212 | "outputs": [],
1213 | "source": [
1214 | "transformed = df['fruit'].map(transform)"
1215 | ]
1216 | },
1217 | {
1218 | "cell_type": "code",
1219 | "execution_count": 26,
1220 | "id": "f310e225",
1221 | "metadata": {
1222 | "slideshow": {
1223 | "slide_type": "fragment"
1224 | }
1225 | },
1226 | "outputs": [
1227 | {
1228 | "data": {
1229 | "text/plain": [
1230 | "0 Apple\n",
1231 | "1 Banana\n",
1232 | "2 Watermelon\n",
1233 | "Name: fruit, dtype: object"
1234 | ]
1235 | },
1236 | "execution_count": 26,
1237 | "metadata": {},
1238 | "output_type": "execute_result"
1239 | }
1240 | ],
1241 | "source": [
1242 | "transformed"
1243 | ]
1244 | },
1245 | {
1246 | "cell_type": "markdown",
1247 | "id": "416f7743",
1248 | "metadata": {
1249 | "slideshow": {
1250 | "slide_type": "slide"
1251 | }
1252 | },
1253 | "source": [
1254 | "## File object\n",
1255 | "\n",
1256 | "- File object in Python provides the main interface to external files\n",
1257 | "- In contrast to other core types, file objects are created not with a literal,\n",
1258 | "- But with a function, `open()`:\n",
1259 | "\n",
1260 | "```\n",
1261 | " = open(, )\n",
1262 | "```"
1263 | ]
1264 | },
1265 | {
1266 | "cell_type": "markdown",
1267 | "id": "56cb2b3e",
1268 | "metadata": {
1269 | "slideshow": {
1270 | "slide_type": "slide"
1271 | }
1272 | },
1273 | "source": [
1274 | "## Data input and output\n",
1275 | "\n",
1276 | "- Modes of file objects allow to:\n",
1277 | " - (`r`)ead a file (default)\n",
1278 | " - (`w`)rite an object to a file\n",
1279 | " - e(`x`)clusively create, failing if a file exists\n",
1280 | " - (`a`)ppend to a file\n",
1281 | "- You can `r+` mode if you need to read and write to file"
1282 | ]
1283 | },
1284 | {
1285 | "cell_type": "markdown",
1286 | "id": "cdc07596",
1287 | "metadata": {
1288 | "slideshow": {
1289 | "slide_type": "slide"
1290 | }
1291 | },
1292 | "source": [
1293 | "## Data output example"
1294 | ]
1295 | },
1296 | {
1297 | "cell_type": "code",
1298 | "execution_count": 27,
1299 | "id": "17d8af2b",
1300 | "metadata": {
1301 | "slideshow": {
1302 | "slide_type": "fragment"
1303 | }
1304 | },
1305 | "outputs": [],
1306 | "source": [
1307 | "f = open('../temp/test.txt', 'w') # Create a new file object in write mode"
1308 | ]
1309 | },
1310 | {
1311 | "cell_type": "code",
1312 | "execution_count": 28,
1313 | "id": "dbc38375",
1314 | "metadata": {
1315 | "slideshow": {
1316 | "slide_type": "fragment"
1317 | }
1318 | },
1319 | "outputs": [
1320 | {
1321 | "data": {
1322 | "text/plain": [
1323 | "20"
1324 | ]
1325 | },
1326 | "execution_count": 28,
1327 | "metadata": {},
1328 | "output_type": "execute_result"
1329 | }
1330 | ],
1331 | "source": [
1332 | "f.write('This is a test file.') # Write a string of characters to it"
1333 | ]
1334 | },
1335 | {
1336 | "cell_type": "code",
1337 | "execution_count": 29,
1338 | "id": "aa0cfde4",
1339 | "metadata": {
1340 | "slideshow": {
1341 | "slide_type": "fragment"
1342 | }
1343 | },
1344 | "outputs": [],
1345 | "source": [
1346 | "f.close() # Flush output buffers to disk and close the connection"
1347 | ]
1348 | },
1349 | {
1350 | "cell_type": "markdown",
1351 | "id": "03fde0bf",
1352 | "metadata": {
1353 | "slideshow": {
1354 | "slide_type": "slide"
1355 | }
1356 | },
1357 | "source": [
1358 | "## Data input example\n",
1359 | "\n",
1360 | "- To avoid keeping track of open file connections, `with` statement can be used\n",
1361 | "\n",
1362 | "Extra: [Python documentation on with statement](https://docs.python.org/3.10/reference/compound_stmts.html#with)"
1363 | ]
1364 | },
1365 | {
1366 | "cell_type": "code",
1367 | "execution_count": 30,
1368 | "id": "75419a09",
1369 | "metadata": {
1370 | "slideshow": {
1371 | "slide_type": "fragment"
1372 | }
1373 | },
1374 | "outputs": [],
1375 | "source": [
1376 | "with open('../temp/test.txt', 'r') as f: # Note that we use 'r' mode for reading\n",
1377 | " text = f.read()"
1378 | ]
1379 | },
1380 | {
1381 | "cell_type": "code",
1382 | "execution_count": 31,
1383 | "id": "845b832f",
1384 | "metadata": {
1385 | "slideshow": {
1386 | "slide_type": "fragment"
1387 | }
1388 | },
1389 | "outputs": [
1390 | {
1391 | "data": {
1392 | "text/plain": [
1393 | "'This is a test file.'"
1394 | ]
1395 | },
1396 | "execution_count": 31,
1397 | "metadata": {},
1398 | "output_type": "execute_result"
1399 | }
1400 | ],
1401 | "source": [
1402 | "text"
1403 | ]
1404 | },
1405 | {
1406 | "cell_type": "markdown",
1407 | "id": "33885b2f",
1408 | "metadata": {
1409 | "slideshow": {
1410 | "slide_type": "slide"
1411 | }
1412 | },
1413 | "source": [
1414 | "## Reading and writing data in `pandas`\n",
1415 | "\n",
1416 | "- `pandas` provides high-level methods that takes care of file connections\n",
1417 | "- These methods all follow the same `read_` and `to_` name patterns\n",
1418 | "- CSV (comma-separated value) files are the standard of interoperability\n",
1419 | "\n",
1420 | "```\n",
1421 | " = pd.read_()\n",
1422 | "```\n",
1423 | "\n",
1424 | "```\n",
1425 | ".to_()\n",
1426 | "```"
1427 | ]
1428 | },
1429 | {
1430 | "cell_type": "markdown",
1431 | "id": "b4b3ddeb",
1432 | "metadata": {
1433 | "slideshow": {
1434 | "slide_type": "slide"
1435 | }
1436 | },
1437 | "source": [
1438 | "## Flights statistics\n",
1439 | "\n",
1440 | "\n",
1441 | " \n",
1442 | "  | \n",
1443 | "  | \n",
1444 | "
\n",
1445 | "
\n",
1446 | "\n",
1447 | "Source: [YouTube](https://www.youtube.com/watch?v=5YGc4zOqozo), [Bureau of Transportation](https://transtats.bts.gov/ONTIME/Departures.aspx)"
1448 | ]
1449 | },
1450 | {
1451 | "cell_type": "markdown",
1452 | "id": "e2de9155",
1453 | "metadata": {
1454 | "slideshow": {
1455 | "slide_type": "slide"
1456 | }
1457 | },
1458 | "source": [
1459 | "## Reading data in `pandas` example\n",
1460 | "\n",
1461 | "- We will use the data from [Bureau of Transportation](https://transtats.bts.gov/ONTIME/Departures.aspx)\n",
1462 | "- Domestic departures from major US airports for United Airlines\n",
1463 | "- Between 1 January and 31 May 2022"
1464 | ]
1465 | },
1466 | {
1467 | "cell_type": "code",
1468 | "execution_count": 32,
1469 | "id": "58f68c1a",
1470 | "metadata": {
1471 | "slideshow": {
1472 | "slide_type": "fragment"
1473 | }
1474 | },
1475 | "outputs": [],
1476 | "source": [
1477 | "united_2022 = pd.read_csv('../data/united_2022.csv')"
1478 | ]
1479 | },
1480 | {
1481 | "cell_type": "markdown",
1482 | "id": "e61f0d51",
1483 | "metadata": {
1484 | "slideshow": {
1485 | "slide_type": "slide"
1486 | }
1487 | },
1488 | "source": [
1489 | "## Basic DataFrame info"
1490 | ]
1491 | },
1492 | {
1493 | "cell_type": "code",
1494 | "execution_count": 33,
1495 | "id": "bef0f82b",
1496 | "metadata": {},
1497 | "outputs": [
1498 | {
1499 | "data": {
1500 | "text/plain": [
1501 | "2"
1502 | ]
1503 | },
1504 | "execution_count": 33,
1505 | "metadata": {},
1506 | "output_type": "execute_result"
1507 | }
1508 | ],
1509 | "source": [
1510 | "# Data dimensionality, 2d for tabular\n",
1511 | "united_2022.ndim"
1512 | ]
1513 | },
1514 | {
1515 | "cell_type": "code",
1516 | "execution_count": 34,
1517 | "id": "22c2e238",
1518 | "metadata": {
1519 | "slideshow": {
1520 | "slide_type": "fragment"
1521 | }
1522 | },
1523 | "outputs": [
1524 | {
1525 | "data": {
1526 | "text/plain": [
1527 | "(154575, 18)"
1528 | ]
1529 | },
1530 | "execution_count": 34,
1531 | "metadata": {},
1532 | "output_type": "execute_result"
1533 | }
1534 | ],
1535 | "source": [
1536 | "# Size of each dimension (analogous to R's dim())\n",
1537 | "# (n_rows, n_columns)\n",
1538 | "united_2022.shape"
1539 | ]
1540 | },
1541 | {
1542 | "cell_type": "code",
1543 | "execution_count": 35,
1544 | "id": "83b73063",
1545 | "metadata": {
1546 | "slideshow": {
1547 | "slide_type": "fragment"
1548 | }
1549 | },
1550 | "outputs": [
1551 | {
1552 | "data": {
1553 | "text/plain": [
1554 | "2782350"
1555 | ]
1556 | },
1557 | "execution_count": 35,
1558 | "metadata": {},
1559 | "output_type": "execute_result"
1560 | }
1561 | ],
1562 | "source": [
1563 | "# Total number of cells (n_rows * n_columns)\n",
1564 | "united_2022.size"
1565 | ]
1566 | },
1567 | {
1568 | "cell_type": "markdown",
1569 | "id": "988c5e31",
1570 | "metadata": {
1571 | "slideshow": {
1572 | "slide_type": "slide"
1573 | }
1574 | },
1575 | "source": [
1576 | "## Visual data inspection"
1577 | ]
1578 | },
1579 | {
1580 | "cell_type": "code",
1581 | "execution_count": 36,
1582 | "id": "cee409f4",
1583 | "metadata": {
1584 | "scrolled": true,
1585 | "slideshow": {
1586 | "slide_type": "fragment"
1587 | }
1588 | },
1589 | "outputs": [
1590 | {
1591 | "data": {
1592 | "text/html": [
1593 | "\n",
1594 | "\n",
1607 | "
\n",
1608 | " \n",
1609 | " \n",
1610 | " | \n",
1611 | " Carrier Code | \n",
1612 | " Date (MM/DD/YYYY) | \n",
1613 | " Flight Number | \n",
1614 | " Tail Number | \n",
1615 | " Origin Airport | \n",
1616 | " Destination Airport | \n",
1617 | " Scheduled departure time | \n",
1618 | " Actual departure time | \n",
1619 | " Scheduled elapsed time (Minutes) | \n",
1620 | " Actual elapsed time (Minutes) | \n",
1621 | " Departure delay (Minutes) | \n",
1622 | " Wheels-off time | \n",
1623 | " Taxi-Out time (Minutes) | \n",
1624 | " Delay Carrier (Minutes) | \n",
1625 | " Delay Weather (Minutes) | \n",
1626 | " Delay National Aviation System (Minutes) | \n",
1627 | " Delay Security (Minutes) | \n",
1628 | " Delay Late Aircraft Arrival (Minutes) | \n",
1629 | "
\n",
1630 | " \n",
1631 | " \n",
1632 | " \n",
1633 | " 0 | \n",
1634 | " UA | \n",
1635 | " 01/01/2022 | \n",
1636 | " 225 | \n",
1637 | " N488UA | \n",
1638 | " ATL | \n",
1639 | " DEN | \n",
1640 | " 16:15:00 | \n",
1641 | " 17:23:00 | \n",
1642 | " 211 | \n",
1643 | " 240 | \n",
1644 | " 68 | \n",
1645 | " 17:35:00 | \n",
1646 | " 12 | \n",
1647 | " 0 | \n",
1648 | " 0 | \n",
1649 | " 29 | \n",
1650 | " 0 | \n",
1651 | " 68 | \n",
1652 | "
\n",
1653 | " \n",
1654 | " 1 | \n",
1655 | " UA | \n",
1656 | " 01/01/2022 | \n",
1657 | " 282 | \n",
1658 | " N447UA | \n",
1659 | " ATL | \n",
1660 | " IAH | \n",
1661 | " 19:00:00 | \n",
1662 | " 19:02:00 | \n",
1663 | " 138 | \n",
1664 | " 126 | \n",
1665 | " 2 | \n",
1666 | " 19:15:00 | \n",
1667 | " 13 | \n",
1668 | " 0 | \n",
1669 | " 0 | \n",
1670 | " 0 | \n",
1671 | " 0 | \n",
1672 | " 0 | \n",
1673 | "
\n",
1674 | " \n",
1675 | " 2 | \n",
1676 | " UA | \n",
1677 | " 01/01/2022 | \n",
1678 | " 340 | \n",
1679 | " N809UA | \n",
1680 | " ATL | \n",
1681 | " DEN | \n",
1682 | " 08:20:00 | \n",
1683 | " 08:17:00 | \n",
1684 | " 211 | \n",
1685 | " 283 | \n",
1686 | " -3 | \n",
1687 | " 08:33:00 | \n",
1688 | " 16 | \n",
1689 | " 0 | \n",
1690 | " 0 | \n",
1691 | " 69 | \n",
1692 | " 0 | \n",
1693 | " 0 | \n",
1694 | "
\n",
1695 | " \n",
1696 | " 3 | \n",
1697 | " UA | \n",
1698 | " 01/02/2022 | \n",
1699 | " 225 | \n",
1700 | " N463UA | \n",
1701 | " ATL | \n",
1702 | " DEN | \n",
1703 | " 16:15:00 | \n",
1704 | " 16:36:00 | \n",
1705 | " 211 | \n",
1706 | " 193 | \n",
1707 | " 21 | \n",
1708 | " 16:48:00 | \n",
1709 | " 12 | \n",
1710 | " 0 | \n",
1711 | " 0 | \n",
1712 | " 0 | \n",
1713 | " 0 | \n",
1714 | " 0 | \n",
1715 | "
\n",
1716 | " \n",
1717 | " 4 | \n",
1718 | " UA | \n",
1719 | " 01/02/2022 | \n",
1720 | " 282 | \n",
1721 | " N63899 | \n",
1722 | " ATL | \n",
1723 | " IAH | \n",
1724 | " 19:00:00 | \n",
1725 | " 18:54:00 | \n",
1726 | " 138 | \n",
1727 | " 129 | \n",
1728 | " -6 | \n",
1729 | " 19:07:00 | \n",
1730 | " 13 | \n",
1731 | " 0 | \n",
1732 | " 0 | \n",
1733 | " 0 | \n",
1734 | " 0 | \n",
1735 | " 0 | \n",
1736 | "
\n",
1737 | " \n",
1738 | "
\n",
1739 | "
"
1740 | ],
1741 | "text/plain": [
1742 | " Carrier Code Date (MM/DD/YYYY) Flight Number Tail Number Origin Airport \\\n",
1743 | "0 UA 01/01/2022 225 N488UA ATL \n",
1744 | "1 UA 01/01/2022 282 N447UA ATL \n",
1745 | "2 UA 01/01/2022 340 N809UA ATL \n",
1746 | "3 UA 01/02/2022 225 N463UA ATL \n",
1747 | "4 UA 01/02/2022 282 N63899 ATL \n",
1748 | "\n",
1749 | " Destination Airport Scheduled departure time Actual departure time \\\n",
1750 | "0 DEN 16:15:00 17:23:00 \n",
1751 | "1 IAH 19:00:00 19:02:00 \n",
1752 | "2 DEN 08:20:00 08:17:00 \n",
1753 | "3 DEN 16:15:00 16:36:00 \n",
1754 | "4 IAH 19:00:00 18:54:00 \n",
1755 | "\n",
1756 | " Scheduled elapsed time (Minutes) Actual elapsed time (Minutes) \\\n",
1757 | "0 211 240 \n",
1758 | "1 138 126 \n",
1759 | "2 211 283 \n",
1760 | "3 211 193 \n",
1761 | "4 138 129 \n",
1762 | "\n",
1763 | " Departure delay (Minutes) Wheels-off time Taxi-Out time (Minutes) \\\n",
1764 | "0 68 17:35:00 12 \n",
1765 | "1 2 19:15:00 13 \n",
1766 | "2 -3 08:33:00 16 \n",
1767 | "3 21 16:48:00 12 \n",
1768 | "4 -6 19:07:00 13 \n",
1769 | "\n",
1770 | " Delay Carrier (Minutes) Delay Weather (Minutes) \\\n",
1771 | "0 0 0 \n",
1772 | "1 0 0 \n",
1773 | "2 0 0 \n",
1774 | "3 0 0 \n",
1775 | "4 0 0 \n",
1776 | "\n",
1777 | " Delay National Aviation System (Minutes) Delay Security (Minutes) \\\n",
1778 | "0 29 0 \n",
1779 | "1 0 0 \n",
1780 | "2 69 0 \n",
1781 | "3 0 0 \n",
1782 | "4 0 0 \n",
1783 | "\n",
1784 | " Delay Late Aircraft Arrival (Minutes) \n",
1785 | "0 68 \n",
1786 | "1 0 \n",
1787 | "2 0 \n",
1788 | "3 0 \n",
1789 | "4 0 "
1790 | ]
1791 | },
1792 | "execution_count": 36,
1793 | "metadata": {},
1794 | "output_type": "execute_result"
1795 | }
1796 | ],
1797 | "source": [
1798 | "united_2022.head() # Returns the top n (n=5 default) rows"
1799 | ]
1800 | },
1801 | {
1802 | "cell_type": "markdown",
1803 | "id": "cf69bf51",
1804 | "metadata": {
1805 | "slideshow": {
1806 | "slide_type": "slide"
1807 | }
1808 | },
1809 | "source": [
1810 | "## Visual data inspection continued"
1811 | ]
1812 | },
1813 | {
1814 | "cell_type": "code",
1815 | "execution_count": 37,
1816 | "id": "3fb006bd",
1817 | "metadata": {
1818 | "slideshow": {
1819 | "slide_type": "fragment"
1820 | }
1821 | },
1822 | "outputs": [
1823 | {
1824 | "data": {
1825 | "text/html": [
1826 | "\n",
1827 | "\n",
1840 | "
\n",
1841 | " \n",
1842 | " \n",
1843 | " | \n",
1844 | " Carrier Code | \n",
1845 | " Date (MM/DD/YYYY) | \n",
1846 | " Flight Number | \n",
1847 | " Tail Number | \n",
1848 | " Origin Airport | \n",
1849 | " Destination Airport | \n",
1850 | " Scheduled departure time | \n",
1851 | " Actual departure time | \n",
1852 | " Scheduled elapsed time (Minutes) | \n",
1853 | " Actual elapsed time (Minutes) | \n",
1854 | " Departure delay (Minutes) | \n",
1855 | " Wheels-off time | \n",
1856 | " Taxi-Out time (Minutes) | \n",
1857 | " Delay Carrier (Minutes) | \n",
1858 | " Delay Weather (Minutes) | \n",
1859 | " Delay National Aviation System (Minutes) | \n",
1860 | " Delay Security (Minutes) | \n",
1861 | " Delay Late Aircraft Arrival (Minutes) | \n",
1862 | "
\n",
1863 | " \n",
1864 | " \n",
1865 | " \n",
1866 | " 154570 | \n",
1867 | " UA | \n",
1868 | " 05/31/2022 | \n",
1869 | " 2652 | \n",
1870 | " N47524 | \n",
1871 | " SFO | \n",
1872 | " BOS | \n",
1873 | " 13:25:00 | \n",
1874 | " 13:19:00 | \n",
1875 | " 354 | \n",
1876 | " 318 | \n",
1877 | " -6 | \n",
1878 | " 13:38:00 | \n",
1879 | " 19 | \n",
1880 | " 0 | \n",
1881 | " 0 | \n",
1882 | " 0 | \n",
1883 | " 0 | \n",
1884 | " 0 | \n",
1885 | "
\n",
1886 | " \n",
1887 | " 154571 | \n",
1888 | " UA | \n",
1889 | " 05/31/2022 | \n",
1890 | " 2655 | \n",
1891 | " N15969 | \n",
1892 | " SFO | \n",
1893 | " EWR | \n",
1894 | " 08:50:00 | \n",
1895 | " 10:19:00 | \n",
1896 | " 327 | \n",
1897 | " 343 | \n",
1898 | " 89 | \n",
1899 | " 10:37:00 | \n",
1900 | " 18 | \n",
1901 | " 89 | \n",
1902 | " 0 | \n",
1903 | " 16 | \n",
1904 | " 0 | \n",
1905 | " 0 | \n",
1906 | "
\n",
1907 | " \n",
1908 | " 154572 | \n",
1909 | " UA | \n",
1910 | " 05/31/2022 | \n",
1911 | " 2657 | \n",
1912 | " N77431 | \n",
1913 | " SFO | \n",
1914 | " PHX | \n",
1915 | " 19:00:00 | \n",
1916 | " 18:52:00 | \n",
1917 | " 119 | \n",
1918 | " 115 | \n",
1919 | " -8 | \n",
1920 | " 19:09:00 | \n",
1921 | " 17 | \n",
1922 | " 0 | \n",
1923 | " 0 | \n",
1924 | " 0 | \n",
1925 | " 0 | \n",
1926 | " 0 | \n",
1927 | "
\n",
1928 | " \n",
1929 | " 154573 | \n",
1930 | " UA | \n",
1931 | " 05/31/2022 | \n",
1932 | " 2669 | \n",
1933 | " N76523 | \n",
1934 | " SFO | \n",
1935 | " SAN | \n",
1936 | " 10:54:00 | \n",
1937 | " 10:57:00 | \n",
1938 | " 101 | \n",
1939 | " 85 | \n",
1940 | " 3 | \n",
1941 | " 11:11:00 | \n",
1942 | " 14 | \n",
1943 | " 0 | \n",
1944 | " 0 | \n",
1945 | " 0 | \n",
1946 | " 0 | \n",
1947 | " 0 | \n",
1948 | "
\n",
1949 | " \n",
1950 | " 154574 | \n",
1951 | " UA | \n",
1952 | " 05/31/2022 | \n",
1953 | " 2670 | \n",
1954 | " N37253 | \n",
1955 | " SFO | \n",
1956 | " TPA | \n",
1957 | " 09:59:00 | \n",
1958 | " 09:52:00 | \n",
1959 | " 310 | \n",
1960 | " 305 | \n",
1961 | " -7 | \n",
1962 | " 10:08:00 | \n",
1963 | " 16 | \n",
1964 | " 0 | \n",
1965 | " 0 | \n",
1966 | " 0 | \n",
1967 | " 0 | \n",
1968 | " 0 | \n",
1969 | "
\n",
1970 | " \n",
1971 | "
\n",
1972 | "
"
1973 | ],
1974 | "text/plain": [
1975 | " Carrier Code Date (MM/DD/YYYY) Flight Number Tail Number \\\n",
1976 | "154570 UA 05/31/2022 2652 N47524 \n",
1977 | "154571 UA 05/31/2022 2655 N15969 \n",
1978 | "154572 UA 05/31/2022 2657 N77431 \n",
1979 | "154573 UA 05/31/2022 2669 N76523 \n",
1980 | "154574 UA 05/31/2022 2670 N37253 \n",
1981 | "\n",
1982 | " Origin Airport Destination Airport Scheduled departure time \\\n",
1983 | "154570 SFO BOS 13:25:00 \n",
1984 | "154571 SFO EWR 08:50:00 \n",
1985 | "154572 SFO PHX 19:00:00 \n",
1986 | "154573 SFO SAN 10:54:00 \n",
1987 | "154574 SFO TPA 09:59:00 \n",
1988 | "\n",
1989 | " Actual departure time Scheduled elapsed time (Minutes) \\\n",
1990 | "154570 13:19:00 354 \n",
1991 | "154571 10:19:00 327 \n",
1992 | "154572 18:52:00 119 \n",
1993 | "154573 10:57:00 101 \n",
1994 | "154574 09:52:00 310 \n",
1995 | "\n",
1996 | " Actual elapsed time (Minutes) Departure delay (Minutes) \\\n",
1997 | "154570 318 -6 \n",
1998 | "154571 343 89 \n",
1999 | "154572 115 -8 \n",
2000 | "154573 85 3 \n",
2001 | "154574 305 -7 \n",
2002 | "\n",
2003 | " Wheels-off time Taxi-Out time (Minutes) Delay Carrier (Minutes) \\\n",
2004 | "154570 13:38:00 19 0 \n",
2005 | "154571 10:37:00 18 89 \n",
2006 | "154572 19:09:00 17 0 \n",
2007 | "154573 11:11:00 14 0 \n",
2008 | "154574 10:08:00 16 0 \n",
2009 | "\n",
2010 | " Delay Weather (Minutes) Delay National Aviation System (Minutes) \\\n",
2011 | "154570 0 0 \n",
2012 | "154571 0 16 \n",
2013 | "154572 0 0 \n",
2014 | "154573 0 0 \n",
2015 | "154574 0 0 \n",
2016 | "\n",
2017 | " Delay Security (Minutes) Delay Late Aircraft Arrival (Minutes) \n",
2018 | "154570 0 0 \n",
2019 | "154571 0 0 \n",
2020 | "154572 0 0 \n",
2021 | "154573 0 0 \n",
2022 | "154574 0 0 "
2023 | ]
2024 | },
2025 | "execution_count": 37,
2026 | "metadata": {},
2027 | "output_type": "execute_result"
2028 | }
2029 | ],
2030 | "source": [
2031 | "united_2022.tail() # Returns the bottom n (n=5 default) rows"
2032 | ]
2033 | },
2034 | {
2035 | "cell_type": "markdown",
2036 | "id": "09ad7c0d",
2037 | "metadata": {
2038 | "slideshow": {
2039 | "slide_type": "slide"
2040 | }
2041 | },
2042 | "source": [
2043 | "## Reading in other (non-`.csv`) data files\n",
2044 | "\n",
2045 | "- Pandas can read in file other than `.csv` (comma-separated value)\n",
2046 | "- Common cases include STATA `.dta`, SPSS `.sav` and SAS `.sas`\n",
2047 | "- Use `pd.read_stata(path)`, `pd.read_spss(path)` and `pd.read_sas(path)`\n",
2048 | "- Check [here](https://pandas.pydata.org/docs/getting_started/intro_tutorials/02_read_write.html) for more examples"
2049 | ]
2050 | },
2051 | {
2052 | "cell_type": "markdown",
2053 | "id": "d2893a52",
2054 | "metadata": {
2055 | "slideshow": {
2056 | "slide_type": "slide"
2057 | }
2058 | },
2059 | "source": [
2060 | "## Writing data out in `pandas`\n",
2061 | "\n",
2062 | "- Note that when writing data out we start with the object name storing the dataset\n",
2063 | "- I.e. `df.to_csv(path)` as opposed to `df = pd.read_csv(path)`\n",
2064 | "- Pandas can also write out into other data formats\n",
2065 | "- E.g. `df.to_excel(path)`, `df.to_stata(path)`"
2066 | ]
2067 | },
2068 | {
2069 | "cell_type": "code",
2070 | "execution_count": 38,
2071 | "id": "a6612a6a",
2072 | "metadata": {
2073 | "slideshow": {
2074 | "slide_type": "fragment"
2075 | }
2076 | },
2077 | "outputs": [],
2078 | "source": [
2079 | "united_2022.to_csv('../temp/united_2022.csv')"
2080 | ]
2081 | },
2082 | {
2083 | "cell_type": "markdown",
2084 | "id": "a80ebd4a",
2085 | "metadata": {
2086 | "slideshow": {
2087 | "slide_type": "slide"
2088 | }
2089 | },
2090 | "source": [
2091 | "## Additional pandas materials\n",
2092 | "\n",
2093 | "Books:\n",
2094 | "\n",
2095 | "- McKinney, Wes. 2017. *Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython*. 2nd ed. Sebastopol, CA: O'Reilly Media \n",
2096 | " \n",
2097 | " **From the original author of the library!**\n",
2098 | "\n",
2099 | "Online:\n",
2100 | "\n",
2101 | "- [Pandas Getting Started Tutorials](https://pandas.pydata.org/docs/getting_started/intro_tutorials/index.html)\n",
2102 | "- [Pandas Documentation](https://pandas.pydata.org/docs/reference/index.html) (intermediate and advanced)"
2103 | ]
2104 | },
2105 | {
2106 | "cell_type": "markdown",
2107 | "id": "af10a8bb",
2108 | "metadata": {
2109 | "slideshow": {
2110 | "slide_type": "slide"
2111 | }
2112 | },
2113 | "source": [
2114 | "## Next\n",
2115 | "\n",
2116 | "- Exploratory data analysis\n",
2117 | "- Data visualization"
2118 | ]
2119 | }
2120 | ],
2121 | "metadata": {
2122 | "celltoolbar": "Slideshow",
2123 | "kernelspec": {
2124 | "display_name": "Python 3",
2125 | "language": "python",
2126 | "name": "python3"
2127 | },
2128 | "language_info": {
2129 | "codemirror_mode": {
2130 | "name": "ipython",
2131 | "version": 3
2132 | },
2133 | "file_extension": ".py",
2134 | "mimetype": "text/x-python",
2135 | "name": "python",
2136 | "nbconvert_exporter": "python",
2137 | "pygments_lexer": "ipython3",
2138 | "version": "3.8.10"
2139 | }
2140 | },
2141 | "nbformat": 4,
2142 | "nbformat_minor": 5
2143 | }
2144 |
--------------------------------------------------------------------------------
/lectures/imgs/Stevens1946_tab1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/Stevens1946_tab1.png
--------------------------------------------------------------------------------
/lectures/imgs/Tufte2001_napoleon.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/Tufte2001_napoleon.png
--------------------------------------------------------------------------------
/lectures/imgs/bts.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/bts.png
--------------------------------------------------------------------------------
/lectures/imgs/butterfly_meme.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/butterfly_meme.jpg
--------------------------------------------------------------------------------
/lectures/imgs/europe_diagram.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/europe_diagram.png
--------------------------------------------------------------------------------
/lectures/imgs/guido.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/guido.gif
--------------------------------------------------------------------------------
/lectures/imgs/jupyter_notebook_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/jupyter_notebook_1.png
--------------------------------------------------------------------------------
/lectures/imgs/jupyter_notebook_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/jupyter_notebook_2.png
--------------------------------------------------------------------------------
/lectures/imgs/kaggle_ide.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/kaggle_ide.png
--------------------------------------------------------------------------------
/lectures/imgs/michael_scott.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/michael_scott.jpg
--------------------------------------------------------------------------------
/lectures/imgs/punchcard.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/punchcard.jpg
--------------------------------------------------------------------------------
/lectures/imgs/python_logo.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/python_logo.png
--------------------------------------------------------------------------------
/lectures/imgs/python_monty.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/python_monty.png
--------------------------------------------------------------------------------
/lectures/imgs/python_snake.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/python_snake.jpg
--------------------------------------------------------------------------------
/lectures/imgs/stats_languages.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/stats_languages.jpg
--------------------------------------------------------------------------------
/lectures/imgs/tidy_data.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/tidy_data.png
--------------------------------------------------------------------------------
/lectures/imgs/tiobe_index.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/tiobe_index.png
--------------------------------------------------------------------------------
/lectures/imgs/united_breaks.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/united_breaks.png
--------------------------------------------------------------------------------
/lectures/imgs/venn_diagram_sets.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/venn_diagram_sets.png
--------------------------------------------------------------------------------
/lectures/imgs/what_a_week.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/what_a_week.jpg
--------------------------------------------------------------------------------
/lectures/imgs/xkcd_163.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/xkcd_163.png
--------------------------------------------------------------------------------
/lectures/imgs/xkcd_353.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/lectures/imgs/xkcd_353.png
--------------------------------------------------------------------------------
/requirements.in:
--------------------------------------------------------------------------------
1 | jupyter
2 | pandas
3 | plotnine
4 | statsmodels
5 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | #
2 | # This file is autogenerated by pip-compile with python 3.8
3 | # To update, run:
4 | #
5 | # pip-compile
6 | #
7 | argon2-cffi==21.3.0
8 | # via notebook
9 | argon2-cffi-bindings==21.2.0
10 | # via argon2-cffi
11 | asttokens==2.0.5
12 | # via stack-data
13 | attrs==21.4.0
14 | # via jsonschema
15 | backcall==0.2.0
16 | # via ipython
17 | beautifulsoup4==4.11.1
18 | # via nbconvert
19 | bleach==5.0.1
20 | # via nbconvert
21 | cffi==1.15.1
22 | # via argon2-cffi-bindings
23 | cycler==0.11.0
24 | # via matplotlib
25 | debugpy==1.6.2
26 | # via ipykernel
27 | decorator==5.1.1
28 | # via ipython
29 | defusedxml==0.7.1
30 | # via nbconvert
31 | entrypoints==0.4
32 | # via
33 | # jupyter-client
34 | # nbconvert
35 | executing==0.9.1
36 | # via stack-data
37 | fastjsonschema==2.16.1
38 | # via nbformat
39 | fonttools==4.34.4
40 | # via matplotlib
41 | importlib-resources==5.9.0
42 | # via jsonschema
43 | ipykernel==6.15.1
44 | # via
45 | # ipywidgets
46 | # jupyter
47 | # jupyter-console
48 | # notebook
49 | # qtconsole
50 | ipython==8.4.0
51 | # via
52 | # ipykernel
53 | # ipywidgets
54 | # jupyter-console
55 | ipython-genutils==0.2.0
56 | # via
57 | # ipywidgets
58 | # notebook
59 | # qtconsole
60 | ipywidgets==7.7.1
61 | # via jupyter
62 | jedi==0.18.1
63 | # via ipython
64 | jinja2==3.1.2
65 | # via
66 | # nbconvert
67 | # notebook
68 | jsonschema==4.7.2
69 | # via nbformat
70 | jupyter==1.0.0
71 | # via -r requirements.in
72 | jupyter-client==7.3.4
73 | # via
74 | # ipykernel
75 | # jupyter-console
76 | # nbclient
77 | # notebook
78 | # qtconsole
79 | jupyter-console==6.4.4
80 | # via jupyter
81 | jupyter-core==4.11.1
82 | # via
83 | # jupyter-client
84 | # nbconvert
85 | # nbformat
86 | # notebook
87 | # qtconsole
88 | jupyterlab-pygments==0.2.2
89 | # via nbconvert
90 | jupyterlab-widgets==1.1.1
91 | # via ipywidgets
92 | kiwisolver==1.4.4
93 | # via matplotlib
94 | markupsafe==2.1.1
95 | # via
96 | # jinja2
97 | # nbconvert
98 | matplotlib==3.5.2
99 | # via
100 | # mizani
101 | # plotnine
102 | matplotlib-inline==0.1.3
103 | # via
104 | # ipykernel
105 | # ipython
106 | mistune==0.8.4
107 | # via nbconvert
108 | mizani==0.7.4
109 | # via plotnine
110 | nbclient==0.6.6
111 | # via nbconvert
112 | nbconvert==6.5.0
113 | # via
114 | # jupyter
115 | # notebook
116 | nbformat==5.4.0
117 | # via
118 | # nbclient
119 | # nbconvert
120 | # notebook
121 | nest-asyncio==1.5.5
122 | # via
123 | # ipykernel
124 | # jupyter-client
125 | # nbclient
126 | # notebook
127 | notebook==6.4.12
128 | # via
129 | # jupyter
130 | # widgetsnbextension
131 | numpy==1.23.1
132 | # via
133 | # matplotlib
134 | # mizani
135 | # pandas
136 | # patsy
137 | # plotnine
138 | # scipy
139 | # statsmodels
140 | packaging==21.3
141 | # via
142 | # ipykernel
143 | # matplotlib
144 | # nbconvert
145 | # qtpy
146 | # statsmodels
147 | palettable==3.3.0
148 | # via mizani
149 | pandas==1.4.3
150 | # via
151 | # -r requirements.in
152 | # mizani
153 | # plotnine
154 | # statsmodels
155 | pandocfilters==1.5.0
156 | # via nbconvert
157 | parso==0.8.3
158 | # via jedi
159 | patsy==0.5.2
160 | # via
161 | # plotnine
162 | # statsmodels
163 | pexpect==4.8.0
164 | # via ipython
165 | pickleshare==0.7.5
166 | # via ipython
167 | pillow==9.2.0
168 | # via matplotlib
169 | plotnine==0.9.0
170 | # via -r requirements.in
171 | prometheus-client==0.14.1
172 | # via notebook
173 | prompt-toolkit==3.0.30
174 | # via
175 | # ipython
176 | # jupyter-console
177 | psutil==5.9.1
178 | # via ipykernel
179 | ptyprocess==0.7.0
180 | # via
181 | # pexpect
182 | # terminado
183 | pure-eval==0.2.2
184 | # via stack-data
185 | pycparser==2.21
186 | # via cffi
187 | pygments==2.12.0
188 | # via
189 | # ipython
190 | # jupyter-console
191 | # nbconvert
192 | # qtconsole
193 | pyparsing==3.0.9
194 | # via
195 | # matplotlib
196 | # packaging
197 | pyrsistent==0.18.1
198 | # via jsonschema
199 | python-dateutil==2.8.2
200 | # via
201 | # jupyter-client
202 | # matplotlib
203 | # pandas
204 | pytz==2022.1
205 | # via pandas
206 | pyzmq==23.2.0
207 | # via
208 | # ipykernel
209 | # jupyter-client
210 | # notebook
211 | # qtconsole
212 | qtconsole==5.3.1
213 | # via jupyter
214 | qtpy==2.1.0
215 | # via qtconsole
216 | scipy==1.8.1
217 | # via
218 | # mizani
219 | # plotnine
220 | # statsmodels
221 | send2trash==1.8.0
222 | # via notebook
223 | six==1.16.0
224 | # via
225 | # asttokens
226 | # bleach
227 | # patsy
228 | # python-dateutil
229 | soupsieve==2.3.2.post1
230 | # via beautifulsoup4
231 | stack-data==0.3.0
232 | # via ipython
233 | statsmodels==0.13.2
234 | # via
235 | # -r requirements.in
236 | # plotnine
237 | terminado==0.15.0
238 | # via notebook
239 | tinycss2==1.1.1
240 | # via nbconvert
241 | tornado==6.2
242 | # via
243 | # ipykernel
244 | # jupyter-client
245 | # notebook
246 | # terminado
247 | traitlets==5.3.0
248 | # via
249 | # ipykernel
250 | # ipython
251 | # ipywidgets
252 | # jupyter-client
253 | # jupyter-core
254 | # matplotlib-inline
255 | # nbclient
256 | # nbconvert
257 | # nbformat
258 | # notebook
259 | # qtconsole
260 | wcwidth==0.2.5
261 | # via prompt-toolkit
262 | webencodings==0.5.1
263 | # via
264 | # bleach
265 | # tinycss2
266 | widgetsnbextension==3.6.1
267 | # via ipywidgets
268 | zipp==3.8.1
269 | # via importlib-resources
270 |
271 | # The following packages are considered to be unsafe in a requirements file:
272 | # setuptools
273 |
--------------------------------------------------------------------------------
/syllabus/Introduction_to_Python_DS3.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/tpaskhalis/DS3_Introduction_Python/c36519e05b58ef50ad60aa6f9549482cbf68b20f/syllabus/Introduction_to_Python_DS3.pdf
--------------------------------------------------------------------------------