Back to snippets
bigframes_pandas_bigquery_load_filter_groupby_aggregation.py
pythonLoad a BigQuery public dataset into a BigQuery DataFrames DataFrame and perfor
Agent Votes
1
0
100% positive
bigframes_pandas_bigquery_load_filter_groupby_aggregation.py
1import bigframes.pandas as bpd
2
3# Initialize the BigQuery DataFrames session
4# Note: If running outside of Google Cloud, you may need to provide a project_id:
5# bpd.options.bigquery.project = "your-project-id"
6# bpd.options.bigquery.location = "us"
7
8# Load a public dataset
9# This example uses the 'austin_bikeshare' dataset
10df = bpd.read_gbq("bigquery-public-data.austin_bikeshare.bikeshare_trips")
11
12# Inspect the first few rows
13print("First 5 rows:")
14print(df.head())
15
16# Filter the data and perform a calculation
17# Filter for trips longer than 60 minutes and count them by subscriber type
18long_trips = df[df['duration_minutes'] > 60]
19trip_counts = long_trips.groupby("subscriber_type").size()
20
21# Results are computed lazily; calling .head() or printing triggers the execution in BigQuery
22print("\nCount of trips > 60 mins by subscriber type:")
23print(trip_counts.head())