This is the recommended way to programmatically access BigQuery when you are in a notebook environment.
If you need to create a Python code that works outside of a notebook environment (e.g. in a production environment), you should use code as shown in the Google Cloud Client Library example notebook in this directory.
In [2]:
#!pip install google-cloud-bigquery
%load_ext google.cloud.bigquery
In [8]:
PROJECT='cloud-training-demos' # CHANGE THIS
In [2]:
%%bigquery --project $PROJECT
SELECT
start_station_name
, AVG(duration) as duration
, COUNT(duration) as num_trips
FROM `bigquery-public-data`.london_bicycles.cycle_hire
GROUP BY start_station_name
ORDER BY num_trips DESC
LIMIT 5
Out[2]:
In [3]:
PARAMS = {"num_stations": 3}
In [4]:
%%bigquery --project $PROJECT --params $PARAMS
SELECT
start_station_name
, AVG(duration) as duration
, COUNT(duration) as num_trips
FROM `bigquery-public-data`.london_bicycles.cycle_hire
GROUP BY start_station_name
ORDER BY num_trips DESC
LIMIT @num_stations
Out[4]:
In [5]:
%%bigquery df --project $PROJECT
SELECT
start_station_name
, AVG(duration) as duration
, COUNT(duration) as num_trips
FROM `bigquery-public-data`.london_bicycles.cycle_hire
GROUP BY start_station_name
ORDER BY num_trips DESC
In [6]:
df.describe()
Out[6]:
In [7]:
df.plot.scatter('duration', 'num_trips');
Copyright 2019 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License