Google Colab 101: Connecting to & Querying BigQuery Data

Song Joyce Park
2 min readSep 17, 2021

The ease and beauty of Colab truly comes to light when you use it along with BigQuery. Both being under the Google umbrella, accessing BigQuery data through Colab is as quick & easy as it can get.

(Note: Besides the method I explain below, there is another method of connecting to BigQuery in Colab using a service account key json file. This method is not explained in this post)

Account Verification

Run the following code:

from google.colab import auth
auth.authenticate_user()

A link and a box asking you to “Enter verification code” will appear. Click this link!

Once you log in with the appropriate account (the one you have BigQuery access to), you will receive a verification code. Copy this code into the Colab notebook in the “Enter verification code” box and press Enter.

Connecting to Your BigQuery Project

To connect to your data in BigQuery, run the following code:

#Replace 'project_id' with your BigQuery project ID

from google.cloud import bigquery
client = bigquery.Client(project='project_id')

You can then use SQL to query your BigQuery data and save the output of the query as a dataframe with the following code:

#Replace 'dataset_name' with your actual dataset name
#Replace 'table_name' with the name of the table you want to query

sql_query = ('''SELECT *
FROM dataset_name.table_name
LIMIT 10''')

df = client.query(sql_query).to_dataframe()

--

--

Song Joyce Park

Data Analyst turned Product Manager. New Yorker turned Seoulite.