Google BigQuery Magics for Jupyter and IPython

0.14.0 · active · verified Thu Apr 16

bigquery-magics provides IPython cell magics that allow users to run SQL queries directly within Jupyter and IPython notebooks against Google BigQuery. It simplifies data exploration by automatically converting query results into pandas DataFrames. The library is part of the actively maintained `google-cloud-python` monorepo and sees frequent updates, with the current stable version being 0.14.0.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the `bigquery_magics` extension, set the Google Cloud project, and execute a BigQuery SQL query directly within a Jupyter cell. The `%%bigquery` cell magic runs the query and automatically saves the results into a pandas DataFrame, specified as a line argument.

# Load the BigQuery magics extension
%load_ext bigquery_magics

# Set your Google Cloud Project ID
# Replace 'your-gcp-project-id' with your actual Project ID.
# Ensure your environment is authenticated (e.g., via gcloud auth application-default login)
import os
os.environ['GOOGLE_CLOUD_PROJECT'] = os.environ.get('GOOGLE_CLOUD_PROJECT', 'your-gcp-project-id')

# Run a BigQuery SQL query using the %%bigquery cell magic
# The results will be stored in a pandas DataFrame named 'df_names'
%%bigquery df_names
SELECT
    name,
    SUM(number) AS count
FROM
    `bigquery-public-data.usa_names.usa_1910_current`
GROUP BY
    name
ORDER BY
    count DESC
LIMIT
    5;

# Display the DataFrame
print(df_names)

view raw JSON →