4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jupyter notebookからBigQueryを使う

Posted at

試した環境

localのdockerでjupyterを起動

$ cat docker-compose.yml
version: "3"
services:
  jupyter:
    image: jupyter/datascience-notebook
    ports:
      - "8888:8888"
    volumes:
      - jupyter_data:/opt/conda
      - jovyan_data:/home/jovyan
    container_name: jupyter
volumes:
  jupyter_data:
  jovyan_data:

必要なパッケージのインストール

jupyter notebookだとブラウザ上からターミナルを呼び出してpip installができる。
001.png

pip install pandas-gbq -U
pip install --upgrade google-api-python-client
pip install --upgrade oauth2client
002.png

BigQueryとの接続

事前にBigQueryにデータセットとテーブルを作成しておく。
※今回は気象庁から横浜市の気温データを取り込んで使用。

# BigQuery接続テスト(standardSQL)
import pandas as pd
query = 'SELECT * FROM dataset.table_name'
data_frame = pd.read_gbq(query, 'project_id', dialect='standard')
005.png

pd.read_gbqを実行した時にURLが表示されるので、そこからgoogle accountの認証を行う。
認証後に表示されるコードを登録することでデータが取得できる。

007.png
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?