Skip to main content

Working in Colab

The course notebooks in your browser, with nothing to install.

Google Colab runs notebooks on a Google machine. You need a Google account. Use it while your installation is not ready.

ImportantFirst of all: save a copy in Drive

The notebook that opens is the shared one, which you cannot change. Choose File, then Save a copy in Drive, and work on your own copy: it stays in your Drive, in the folder Colab Notebooks.

Open a session

  1. In Sessions, press Open in Colab.
  2. Save your copy (box above). It opens in a new tab: continue there.
  3. Run the first cell. It downloads the data of the session, installs the course packages and restarts once. Run it again after the restart: it prints Ready.
  4. Run the rest as on a laptop: the working directory is the session folder and data/... is there.

Next time, open your copy from your Drive and run the first cell again: the files of a Colab session disappear when it ends, and it ends after some time without activity. Your notebook stays in Drive.

Your data in Colab: three ways

The first cell of the course notebooks uses the first way for you. For your own notebooks and your project, choose one.

Read the file straight from the web. Nothing to upload, nothing to lose.

import pandas as pd
wdi = pd.read_csv("https://albarran.github.io/DMA/S01_pandas_I/data/wdi_panel.csv")

Or bring a whole session and move into its folder:

!wget -q https://albarran.github.io/DMA/sessions/S01_pandas_I.zip
!unzip -q -o S01_pandas_I.zip
%cd DMA/S01_pandas_I
  1. Click the folder icon on the left, then the upload icon, and choose your files (in the example below, a file of yours called brics.csv). They land in /content.
  2. Read them from there, or set the working directory first.
%cd /content
brics = pd.read_csv("brics.csv")

Uploaded files disappear with the session: upload them again next time.

Keep a folder DMA in your Drive with the sessions extracted in it. Then, in the notebook:

from google.colab import drive
drive.mount("/content/drive")
%cd /content/drive/MyDrive/DMA/S01_pandas_I

Colab asks for permission the first time. Files in Drive stay there, and what the notebook writes is saved in Drive.

Check where you are at any time:

import os
os.getcwd()

Packages

Colab has pandas, numpy, matplotlib and many others. Install what is missing in a cell, again in each session:

%pip install -q fredapi wbgapi sdmx1 eurostat pymysql rapidfuzz missingno ollama

R in Colab

  • The R notebooks of the course: in Sessions, press Open in Colab (R), save your copy and run the first cell. It downloads the data of the session and prints Ready; there is no restart. The runtime is already R.
  • Open a new R notebook. In any other notebook: Runtime, then Change runtime type, then R. A notebook has one runtime: every code cell is then R.
  • Packages, again in each session:
install.packages(c("fixest", "wooldridge"))
  • Data: the same three ways. From a URL:
wdi <- read.csv("https://albarran.github.io/DMA/S01_pandas_I/data/wdi_panel.csv")

Or bring a whole session and move into its folder:

download.file("https://albarran.github.io/DMA/sessions/S01_pandas_I.zip", "S01.zip", mode = "wb")
unzip("S01.zip"); setwd("DMA/S01_pandas_I")