Commit 51cdfff4 authored by Simon Clarke's avatar Simon Clarke
Browse files

Added solutions for three ADS2002 notebooks.

parent 3ac648b5
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
%% Cell type:markdown id:04ae1a26 tags:
# Introduction to Google Colab
%% Cell type:markdown id:49f1c223 tags:
Here we will cover the basic of accessing data and filesystem navigation on Google Colab ( This includes:
* Accessing notebooks in Google Drive,
* Sharing notebooks,
* Uploading and downloading data,
* Accessing data via a git repository,
* Accessing data via Google Drive.
For further information, see the online help in Colab or pages such as, which has information on how to access Cloud Data on Amazon and Kaggle servers.
%% Cell type:markdown id:1233ec87 tags:
## Accessing notebooks in Google Drive
%% Cell type:markdown id:df220a4f tags:
When you first open Colab you will see a splash screen which gives you the following options to open a notebook:
* Examples (preloaded notebooks with help on how to use Colab and code snippets),
* Recent (your recent activity),
* Google Drive (files you have saved or uploaded to Google Drive),
* Github (files on a github repository),
* Upload (files to upload from your local machine).
To get started, read the [Introduction to Colab](, which is the first file in `Examples`.
To get used to dealing with notebooks and data download this [notebook]( and [data file](, and then upload the notebook to Colab.
The interface for Colab is fairly similar to Jupyter, and most of the commands that you use in Jupyter are transferrable to Colab. At the top there are two buttons: **+Code** and **+Text**, to add Python code and Markdown text respectively. When you are editing a cell, there is a menubar of useful commands that appears in the upper right-hand side of the cell. While when your are editing a text cell, there is a second menubar which appears at the upper left-hand side of the cell and a real-time preview on the right-hand side. This preview can be toggled, so it is below the Markdown code. The completion feature for editing of code is particularly useful. This will suggest possible completions for functions and data structures.
On the left of each cell, you will see an arrowhead in a circle to run the cell. Press this and once the cell calculations have been completed a green tick will appear, along with the time taken to run the cell. Alternatively, you can run a cell by focussing on the cell and pressing `Shift + Enter`. You can also use the commands in the **Runtime** menu to run individual cells, or run all of the cells.
Click on the first cell of the notebook and try to run all the cells. You should get an error that it can't find the data file. Goto the left-side panel and click on the folder icon. That will open an interface which shows a folder called _sample_data_, and three icons above this. Click on **Upload** and upload the csv file you previously downloaded. You might get a warning that the data will only be available for this session. Once the file is uploaded, click on the three dots at the side and select **Copy path**, then paste this in the `pd.read_csv` command that failed. You should now get that the path has _/content/_ prepended to the filename.
Now try running the notebook again. It should run without any errors.
Shell escapes are commands which are preceeded by a `!`. These run shell commands which interact with the computer's operating system, rather than Python commands. On Colab these are Unix or Linux commands.
If you type (pwd => print working directory)
in a cell, it will give you the name of the current directory or folder that you are working in. This should return _/content_, which is the default directory that Colab opens in. Now we can open files by giving their absolute or relative path. Just before we gave the absolute path, i.e., the path from the top of the directory structure, which always starts with _/_. The relative path is the path relative to the current directory, this will not include _/_ at the beginning. Since the file _IDV60901.94866.csv_ is in the current directory, we could use just this field to open the file. Change `pd.read_csv('/content/IDV60901.94866.csv')` to `pd.read_csv('IDV60901.94866.csv')` and rerun the notebook.
In the directory _sample_data_ there is a file _california_housing_test.csv_. To open this file from the current directory (_/content_) we can specify the absolute path
cht = pd.read_csv('/content/sample_data/IDV60901.94866.csv')
or the relative path
cht = pd.read_csv('sample_data/IDV60901.94866.csv')
You could also open the file by first moving into the directory:
%cd sample_data
cht = pd.read_csv('california_housing_test.csv')
Here we have used the magic command `%cd`, rather than the shell escape `!cd`, as the shell escape only applies for that command, whereas the magic command permanently changes our working directory.
To change back to the original directory you can use (the double dots represent the parent directory of the current directory)
%cd /content
%cd ..
Check your directory after entering these commands using `!pwd`.
To see a listing of the files in the current directory, use
Other shell commands that you might find useful are `mkdir`, `rm`, `mv`, `cp` and `man`.
`mkdir` will create a directory in the current directory using the syntax :
!mkdir newdir
`rm` will remove a file or a directory. To remove a file use:
!rm filename
To remove an empty directory use:
!rm -d newdir
and to remove a directory and any files or directory contained in this directory (be careful about doing this, as there is no going back) use
!rm -r newdir
`mv` will rename or move a file or directory. We will just consider files, but the syntax for directories is the same. To rename a file, but keep it in the same directory, use
!mv filename anotherfile
To move a file to a different directory use
!mv filename pathdir
where pathdir is the absolute or relative path of the directory.
`cp` is similar to `mv`, except that it makes a copy of the file or directory.
`man` display the manual page for the command. For example, to find out everything your need to know about the `rm` command, type
!man rm
To obtain a brief listing of the usage of the command use the `--help` option. For example,
!rm --help
These are always useful, as it is too difficult to remember all the commands and options.
%% Cell type:markdown id:bf01e728 tags:
%% Cell type:markdown id:297c70ee tags:
You can also upload and download files using the Python cells. The following code will upload files and store them in the current directory. Then any of the files can be accessed as normal.
from google.colab import files
To download files, we can use the `` method. For example, to download the file _california_housing_test.csv_ to our default Download directory on our local machine, we would use'/content/sample_data/california_housing_test.csv')
Now goto to the **File** menu. From there you can download the current notebook in various formats. However, click on **Locate in Drive**, which will open a window which shows that the file is saved in _My Drive > Colab Notebooks_.
Goto **Runtime > Manage session** and terminate the current session. Since sessions have an idle time-limit of 90 minutes and a maximum runtime-limit of 12 hours, this simulates starting a new session the next day. Then goto **File > Open Notebook** and open the notebook that was just closed using **Recent** or **Google Drive**. Once the session opens, open the file explorer on the left-hand side. Now the csv file no longer appears. To run this notebook again, you will need to upload the csv file using the methods described above.
You could also open the notebook by going to the web interface for Google Drive and navigating to _My Drive > Colab Notebooks_. Then click on the notebook and Colab will open. Notebook files don't need to be stored in _Colab Notebooks_. Any notebook in Google Drive will be opened in Colab.
You can't open notebooks using the file explorer on Colab, this is only for accessing data.
%% Cell type:markdown id:c6e6d17d tags:
## Sharing notebooks
%% Cell type:markdown id:ec586598 tags:
Sharing notebooks works the same as sharing files in Google Drive. Click **Share** in the upper right-hand corner and this will open a panel, where you can share the notebook by entering the person's email address.
Sharing is a bit like using a git repository, but without the flexibility. You will need to determine a workflow for managing files. The simplest approach is to work on a copy of the file, and then let one person in your group integrate the changes.
%% Cell type:markdown id:8a4d1dd5 tags:
## Accessing data via a git repository
%% Cell type:markdown id:08cdb1b0 tags:
If your data is in a git repository, you can access it by cloning the repository to your current directory. Last semester you should have created a git repository on of the form, which we will refer to as `giturl`. Use your repository name wherever giturl occurs in the following cells and replace `filename`, `username`, `reponame` and `personalaccesstoken` by your personal details.
To clone that repository to the current directory we can use
!git clone giturl
Once this is completed all your files in the repository will have been uploaded to the Colab filesystem in your current directory and can be accessed either using the file explorer or the Python interface.
This process is the same as choosing the **Gitlab** option when opening a file.
Now change directory into your git repository, and create a file. This could be done by saving data using `pd.to_csv()`, copying a file from another directory or using (`touch` creates an empty file):
!touch filename
In a new window in your browser goto your github repository and create a Personal Access Token by following the instructions at Then in Colab change back to the top directory of your git repository and upload the changes you have made on Colab to using the following commands (just make sure you don't store the notebook with the Personal Access Token included):
!git status
!git add filename
!git commit -m 'My message'
!git push
We can also clone public repositories. For example, to clone the repository with the BADS notebooks, we can use:
!git clone
However, as this is public, we cannot upload any changes that are made locally to the repository. If you want to save the changes to the filesystem, you will need to download the files or copy the filesystem to Google Drive.
One last trick is that we can open any public notebook on github by copy the link to the file and changing `` to ``. For example, try opening the file in Colab.
%% Cell type:markdown id:28cee816 tags:
## Accessing data via Google Drive
%% Cell type:markdown id:9ee0f6b0 tags:
The most effective way for accessing data is using Google Drive. We can mount the drive or connect it to the Colab filesystem using the file explorer or the Python interface.
To mount Google Drive using the file explorer, click on the **Google Drive** icon, which is the third icon at the top of the file explorer. This will insert the following codelet into your notebook:
from google.colab import drive
You will then need to run the cell and follow the instructions to generate an authorization code to copy into the cell. Then your Google Drive files will appear at _/content/drive_, with _Shareddrive_ and _My Drive_ folders under these directories.
The second way to mount the drive is just to copy the above codelet into a cell in one of your notebooks, which will then be run automatically each time you run the notebook.
Once you have mounted Google Drive you can then navigate the directories and files using the file explorer or using the commands explained above.
%% Cell type:markdown id:f37225ba tags:
## Exercises
%% Cell type:markdown id:19eda830 tags:
You will need to complete the exercises in Colab. Upload this file to Colab, then when complete download the notebook to your local computer and then submit via Moodle.
%% Cell type:markdown id:e3d3d10c tags:
### Exercise 1 (2 marks)
%% Cell type:markdown id:8a363d77 tags:
The following is a codelet from Upload the data file from the directory for this notebook and then run this code.
Expectation is that this is done through the command line, but could be done via the file explorer.
%% Cell type:code id:8IeHrbz-92yS tags:
``` python
from google.colab import files
%%%% Output: display_data
%%%% Output: execute_result
{'pedestrians-september.csv': b'TimeOfDay,Melbourne Central\n2020-09-01 00:00:00,59.0\n2020-09-01 01:00:00,33.0\n2020-09-01 02:00:00,43.0\n2020-09-01 03:00:00,37.0\n2020-09-01 04:00:00,22.0\n2020-09-01 05:00:00,31.0\n2020-09-01 06:00:00,65.0\n2020-09-01 07:00:00,80.0\n2020-09-01 08:00:00,72.0\n2020-09-01 09:00:00,110.0\n2020-09-01 10:00:00,124.0\n2020-09-01 11:00:00,179.0\n2020-09-01 12:00:00,276.0\n2020-09-01 13:00:00,282.0\n2020-09-01 14:00:00,238.0\n2020-09-01 15:00:00,293.0\n2020-09-01 16:00:00,277.0\n2020-09-01 17:00:00,322.0\n2020-09-01 18:00:00,264.0\n2020-09-01 19:00:00,192.0\n2020-09-01 20:00:00,92.0\n2020-09-01 21:00:00,66.0\n2020-09-01 22:00:00,50.0\n2020-09-01 23:00:00,45.0\n2020-09-02 00:00:00,34.0\n2020-09-02 01:00:00,22.0\n2020-09-02 02:00:00,18.0\n2020-09-02 03:00:00,6.0\n2020-09-02 04:00:00,18.0\n2020-09-02 05:00:00,27.0\n2020-09-02 06:00:00,45.0\n2020-09-02 07:00:00,56.0\n2020-09-02 08:00:00,69.0\n2020-09-02 09:00:00,103.0\n2020-09-02 10:00:00,133.0\n2020-09-02 11:00:00,165.0\n2020-09-02 12:00:00,224.0\n2020-09-02 13:00:00,253.0\n2020-09-02 14:00:00,270.0\n2020-09-02 15:00:00,269.0\n2020-09-02 16:00:00,273.0\n2020-09-02 17:00:00,299.0\n2020-09-02 18:00:00,301.0\n2020-09-02 19:00:00,173.0\n2020-09-02 20:00:00,90.0\n2020-09-02 21:00:00,71.0\n2020-09-02 22:00:00,67.0\n2020-09-02 23:00:00,87.0\n2020-09-03 00:00:00,57.0\n2020-09-03 01:00:00,13.0\n2020-09-03 02:00:00,15.0\n2020-09-03 03:00:00,14.0\n2020-09-03 04:00:00,8.0\n2020-09-03 05:00:00,25.0\n2020-09-03 06:00:00,90.0\n2020-09-03 07:00:00,70.0\n2020-09-03 08:00:00,83.0\n2020-09-03 09:00:00,114.0\n2020-09-03 10:00:00,140.0\n2020-09-03 11:00:00,216.0\n2020-09-03 12:00:00,209.0\n2020-09-03 13:00:00,268.0\n2020-09-03 14:00:00,280.0\n2020-09-03 15:00:00,247.0\n2020-09-03 16:00:00,276.0\n2020-09-03 17:00:00,307.0\n2020-09-03 18:00:00,311.0\n2020-09-03 19:00:00,187.0\n2020-09-03 20:00:00,93.0\n2020-09-03 21:00:00,87.0\n2020-09-03 22:00:00,73.0\n2020-09-03 23:00:00,43.0\n2020-09-04 00:00:00,43.0\n2020-09-04 01:00:00,76.0\n2020-09-04 02:00:00,20.0\n2020-09-04 03:00:00,16.0\n2020-09-04 04:00:00,39.0\n2020-09-04 05:00:00,32.0\n2020-09-04 06:00:00,70.0\n2020-09-04 07:00:00,86.0\n2020-09-04 08:00:00,66.0\n2020-09-04 09:00:00,111.0\n2020-09-04 10:00:00,159.0\n2020-09-04 11:00:00,249.0\n2020-09-04 12:00:00,312.0\n2020-09-04 13:00:00,291.0\n2020-09-04 14:00:00,306.0\n2020-09-04 15:00:00,348.0\n2020-09-04 16:00:00,313.0\n2020-09-04 17:00:00,349.0\n2020-09-04 18:00:00,354.0\n2020-09-04 19:00:00,233.0\n2020-09-04 20:00:00,79.0\n2020-09-04 21:00:00,106.0\n2020-09-04 22:00:00,80.0\n2020-09-04 23:00:00,52.0\n2020-09-05 00:00:00,26.0\n2020-09-05 01:00:00,35.0\n2020-09-05 02:00:00,31.0\n2020-09-05 03:00:00,16.0\n2020-09-05 04:00:00,22.0\n2020-09-05 05:00:00,31.0\n2020-09-05 06:00:00,37.0\n2020-09-05 07:00:00,45.0\n2020-09-05 08:00:00,67.0\n2020-09-05 09:00:00,93.0\n2020-09-05 10:00:00,175.0\n2020-09-05 11:00:00,247.0\n2020-09-05 12:00:00,265.0\n2020-09-05 13:00:00,349.0\n2020-09-05 14:00:00,314.0\n2020-09-05 15:00:00,412.0\n2020-09-05 16:00:00,384.0\n2020-09-05 17:00:00,290.0\n2020-09-05 18:00:00,366.0\n2020-09-05 19:00:00,207.0\n2020-09-05 20:00:00,106.0\n2020-09-05 21:00:00,84.0\n2020-09-05 22:00:00,63.0\n2020-09-05 23:00:00,54.0\n2020-09-06 00:00:00,46.0\n2020-09-06 01:00:00,27.0\n2020-09-06 02:00:00,33.0\n2020-09-06 03:00:00,34.0\n2020-09-06 04:00:00,20.0\n2020-09-06 05:00:00,31.0\n2020-09-06 06:00:00,37.0\n2020-09-06 07:00:00,30.0\n2020-09-06 08:00:00,53.0\n2020-09-06 09:00:00,104.0\n2020-09-06 10:00:00,163.0\n2020-09-06 11:00:00,221.0\n2020-09-06 12:00:00,323.0\n2020-09-06 13:00:00,369.0\n2020-09-06 14:00:00,391.0\n2020-09-06 15:00:00,372.0\n2020-09-06 16:00:00,353.0\n2020-09-06 17:00:00,304.0\n2020-09-06 18:00:00,317.0\n2020-09-06 19:00:00,161.0\n2020-09-06 20:00:00,91.0\n2020-09-06 21:00:00,73.0\n2020-09-06 22:00:00,44.0\n2020-09-06 23:00:00,53.0\n2020-09-07 00:00:00,39.0\n2020-09-07 01:00:00,20.0\n2020-09-07 02:00:00,25.0\n2020-09-07 03:00:00,13.0\n2020-09-07 04:00:00,11.0\n2020-09-07 05:00:00,22.0\n2020-09-07 06:00:00,48.0\n2020-09-07 07:00:00,80.0\n2020-09-07 08:00:00,94.0\n2020-09-07 09:00:00,113.0\n2020-09-07 10:00:00,161.0\n2020-09-07 11:00:00,219.0\n2020-09-07 12:00:00,254.0\n2020-09-07 13:00:00,295.0\n2020-09-07 14:00:00,363.0\n2020-09-07 15:00:00,289.0\n2020-09-07 16:00:00,295.0\n2020-09-07 17:00:00,289.0\n2020-09-07 18:00:00,343.0\n2020-09-07 19:00:00,229.0\n2020-09-07 20:00:00,90.0\n2020-09-07 21:00:00,60.0\n2020-09-07 22:00:00,45.0\n2020-09-07 23:00:00,57.0\n2020-09-08 00:00:00,40.0\n2020-09-08 01:00:00,35.0\n2020-09-08 02:00:00,34.0\n2020-09-08 03:00:00,26.0\n2020-09-08 04:00:00,19.0\n2020-09-08 05:00:00,32.0\n2020-09-08 06:00:00,37.0\n2020-09-08 07:00:00,78.0\n2020-09-08 08:00:00,110.0\n2020-09-08 09:00:00,101.0\n2020-09-08 10:00:00,126.0\n2020-09-08 11:00:00,177.0\n2020-09-08 12:00:00,241.0\n2020-09-08 13:00:00,206.0\n2020-09-08 14:00:00,194.0\n2020-09-08 15:00:00,221.0\n2020-09-08 16:00:00,252.0\n2020-09-08 17:00:00,294.0\n2020-09-08 18:00:00,298.0\n2020-09-08 19:00:00,220.0\n2020-09-08 20:00:00,67.0\n2020-09-08 21:00:00,70.0\n2020-09-08 22:00:00,48.0\n2020-09-08 23:00:00,43.0\n2020-09-09 00:00:00,39.0\n2020-09-09 01:00:00,25.0\n2020-09-09 02:00:00,21.0\n2020-09-09 03:00:00,13.0\n2020-09-09 04:00:00,15.0\n2020-09-09 05:00:00,29.0\n2020-09-09 06:00:00,40.0\n2020-09-09 07:00:00,76.0\n2020-09-09 08:00:00,95.0\n2020-09-09 09:00:00,120.0\n2020-09-09 10:00:00,163.0\n2020-09-09 11:00:00,198.0\n2020-09-09 12:00:00,260.0\n2020-09-09 13:00:00,293.0\n2020-09-09 14:00:00,267.0\n2020-09-09 15:00:00,332.0\n2020-09-09 16:00:00,322.0\n2020-09-09 17:00:00,334.0\n2020-09-09 18:00:00,307.0\n2020-09-09 19:00:00,198.0\n2020-09-09 20:00:00,96.0\n2020-09-09 21:00:00,82.0\n2020-09-09 22:00:00,91.0\n2020-09-09 23:00:00,54.0\n2020-09-10 00:00:00,58.0\n2020-09-10 01:00:00,19.0\n2020-09-10 02:00:00,25.0\n2020-09-10 03:00:00,17.0\n2020-09-10 04:00:00,9.0\n2020-09-10 05:00:00,17.0\n2020-09-10 06:00:00,58.0\n2020-09-10 07:00:00,88.0\n2020-09-10 08:00:00,93.0\n2020-09-10 09:00:00,134.0\n2020-09-10 10:00:00,156.0\n2020-09-10 11:00:00,248.0\n2020-09-10 12:00:00,289.0\n2020-09-10 13:00:00,288.0\n2020-09-10 14:00:00,305.0\n2020-09-10 15:00:00,292.0\n2020-09-10 16:00:00,367.0\n2020-09-10 17:00:00,352.0\n2020-09-10 18:00:00,344.0\n2020-09-10 19:00:00,218.0\n2020-09-10 20:00:00,101.0\n2020-09-10 21:00:00,103.0\n2020-09-10 22:00:00,58.0\n2020-09-10 23:00:00,74.0\n2020-09-11 00:00:00,45.0\n2020-09-11 01:00:00,28.0\n2020-09-11 02:00:00,37.0\n2020-09-11 03:00:00,38.0\n2020-09-11 04:00:00,20.0\n2020-09-11 05:00:00,23.0\n2020-09-11 06:00:00,41.0\n2020-09-11 07:00:00,85.0\n2020-09-11 08:00:00,126.0\n2020-09-11 09:00:00,135.0\n2020-09-11 10:00:00,164.0\n2020-09-11 11:00:00,230.0\n2020-09-11 12:00:00,322.0\n2020-09-11 13:00:00,325.0\n2020-09-11 14:00:00,310.0\n2020-09-11 15:00:00,259.0\n2020-09-11 16:00:00,312.0\n2020-09-11 17:00:00,417.0\n2020-09-11 18:00:00,400.0\n2020-09-11 19:00:00,220.0\n2020-09-11 20:00:00,98.0\n2020-09-11 21:00:00,95.0\n2020-09-11 22:00:00,116.0\n2020-09-11 23:00:00,54.0\n2020-09-12 00:00:00,50.0\n2020-09-12 01:00:00,33.0\n2020-09-12 02:00:00,42.0\n2020-09-12 03:00:00,17.0\n2020-09-12 04:00:00,6.0\n2020-09-12 05:00:00,11.0\n2020-09-12 06:00:00,26.0\n2020-09-12 07:00:00,41.0\n2020-09-12 08:00:00,68.0\n2020-09-12 09:00:00,79.0\n2020-09-12 10:00:00,139.0\n2020-09-12 11:00:00,196.0\n2020-09-12 12:00:00,244.0\n2020-09-12 13:00:00,329.0\n2020-09-12 14:00:00,283.0\n2020-09-12 15:00:00,271.0\n2020-09-12 16:00:00,227.0\n2020-09-12 17:00:00,218.0\n2020-09-12 18:00:00,238.0\n2020-09-12 19:00:00,202.0\n2020-09-12 20:00:00,67.0\n2020-09-12 21:00:00,66.0\n2020-09-12 22:00:00,55.0\n2020-09-12 23:00:00,38.0\n2020-09-13 00:00:00,20.0\n2020-09-13 01:00:00,27.0\n2020-09-13 02:00:00,10.0\n2020-09-13 03:00:00,14.0\n2020-09-13 04:00:00,9.0\n2020-09-13 05:00:00,34.0\n2020-09-13 06:00:00,34.0\n2020-09-13 07:00:00,41.0\n2020-09-13 08:00:00,75.0\n2020-09-13 09:00:00,95.0\n2020-09-13 10:00:00,182.0\n2020-09-13 11:00:00,275.0\n2020-09-13 12:00:00,415.0\n2020-09-13 13:00:00,450.0\n2020-09-13 14:00:00,475.0\n2020-09-13 15:00:00,437.0\n2020-09-13 16:00:00,400.0\n2020-09-13 17:00:00,411.0\n2020-09-13 18:00:00,343.0\n2020-09-13 19:00:00,215.0\n2020-09-13 20:00:00,70.0\n2020-09-13 21:00:00,85.0\n2020-09-13 22:00:00,73.0\n2020-09-13 23:00:00,46.0\n2020-09-14 00:00:00,43.0\n2020-09-14 01:00:00,48.0\n2020-09-14 02:00:00,39.0\n2020-09-14 03:00:00,13.0\n2020-09-14 04:00:00,12.0\n2020-09-14 05:00:00,12.0\n2020-09-14 06:00:00,52.0\n2020-09-14 07:00:00,72.0\n2020-09-14 08:00:00,100.0\n2020-09-14 09:00:00,123.0\n2020-09-14 10:00:00,178.0\n2020-09-14 11:00:00,230.0\n2020-09-14 12:00:00,289.0\n2020-09-14 13:00:00,272.0\n2020-09-14 14:00:00,254.0\n2020-09-14 15:00:00,319.0\n2020-09-14 16:00:00,302.0\n2020-09-14 17:00:00,367.0\n2020-09-14 18:00:00,366.0\n2020-09-14 19:00:00,252.0\n2020-09-14 20:00:00,181.0\n2020-09-14 21:00:00,74.0\n2020-09-14 22:00:00,63.0\n2020-09-14 23:00:00,56.0\n2020-09-15 00:00:00,39.0\n2020-09-15 01:00:00,25.0\n2020-09-15 02:00:00,23.0\n2020-09-15 03:00:00,14.0\n2020-09-15 04:00:00,12.0\n2020-09-15 05:00:00,19.0\n2020-09-15 06:00:00,44.0\n2020-09-15 07:00:00,81.0\n2020-09-15 08:00:00,134.0\n2020-09-15 09:00:00,118.0\n2020-09-15 10:00:00,156.0\n2020-09-15 11:00:00,205.0\n2020-09-15 12:00:00,302.0\n2020-09-15 13:00:00,286.0\n2020-09-15 14:00:00,301.0\n2020-09-15 15:00:00,321.0\n2020-09-15 16:00:00,315.0\n2020-09-15 17:00:00,374.0\n2020-09-15 18:00:00,337.0\n2020-09-15 19:00:00,283.0\n2020-09-15 20:00:00,160.0\n2020-09-15 21:00:00,75.0\n2020-09-15 22:00:00,77.0\n2020-09-15 23:00:00,52.0\n'}
%% Cell type:code id:HPEtCIe495Ig tags:
``` python
%% Cell type:code id:3326d966 tags:
``` python
import pathlib
import pandas as pd
# Read from csv file and set up a datetime index with the correct type.
data = (
.assign(TimeOfDay=lambda df: pd.to_datetime(df['TimeOfDay']))
.rename(columns={"TimeOfDay": "Timestamp"})
%%%% Output: execute_result
Melbourne Central
2020-09-01 00:00:00 59.0
2020-09-01 01:00:00 33.0
2020-09-01 02:00:00 43.0
2020-09-01 03:00:00 37.0
2020-09-01 04:00:00 22.0
%% Cell type:markdown id:2d09977d tags:
### Exercise 2 (3 marks)
%% Cell type:markdown id:6ae507a2 tags:
In your home directory on Colab (_/content_), using one call to `mkdir` create a directory _data/pedestrians_ and copy the data file from Exercise 1 into this directory. Change into this directory and create a README file with a one line description by using `echo` and `>`. The latter redirects the output of a command to a file. For example, `!pwd > WorkingDirectory` will create a file called `WorkingDirectory` containing the name of the current directory.
Last two lines are not required.
%% Cell type:markdown id:luM1umhf-jKB tags:
%% Cell type:code id:2920fbc9 tags:
``` python
!mkdir -p data/pedestrians
!cp pedestrians-september.csv data/pedestrians/
%cd data/pedestrians
!echo This is a README file > README
%% Cell type:markdown id:405149d3 tags:
### Exercise 3 (3 marks)
%% Cell type:markdown id:8e32b5b0 tags:
Clone your github repository and create a text file in the top directory of this repository, then commit these changes to the local repository (don't commit the changes to the remote repository). If you don't have a github repository, use a public repository.
%% Cell type:code id:bb54c276 tags:
``` python
%cd /content/
!git clone
%cd bads-repo/
!touch textfile.txt
!git config --global ""
!git config --global "Simon Clarke"
!git status
!git add textfile.txt
!git commit -m 'Added an empty textfile'
%% Cell type:markdown id:f5a474e3 tags:
### Exercise 4 (2 marks)
%% Cell type:markdown id:d73f4e3b tags:
Mount your Google Drive, change directory to where the Colab notebooks are stored and list the files with a long listing, ordered from newest to oldest.
Again, could mount drive using file explorer. They will need to cope with the space in "Colab Notebooks".
%% Cell type:code id:SyFX4cROAny6 tags:
``` python
from google.colab import drive
%% Cell type:code id:2Bo18gfdBAAp tags:
``` python
%cd /content/drive/MyDrive/Colab\ Notebooks/
%% Cell type:code id:qaFZ3tRtBkLv tags:
``` python
!ls -lt
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment