从沙盒将公开表数据下载到 DataFrame

使用 BigQuery Storage API 将查询结果下载到 DataFrame。

代码示例

Python

试用此示例之前,请按照 BigQuery 快速入门:使用客户端库中的 Python 设置说明进行操作。如需了解详情,请参阅 BigQuery Python API 参考文档

如需向 BigQuery 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为客户端库设置身份验证


from google.cloud import bigquery

# Construct a BigQuery client object.
client = bigquery.Client()

# `SELECT *` is an anti-pattern in BigQuery because it is cheaper and
# faster to use the BigQuery Storage API directly, but BigQuery Sandbox
# users can only use the BigQuery Storage API to download query results.
query_string = "SELECT * FROM `bigquery-public-data.usa_names.usa_1910_current`"

# Use the BigQuery Storage API to speed-up downloads of large tables.
dataframe = client.query_and_wait(query_string).to_dataframe(
    create_bqstorage_client=True
)

print(dataframe.info())

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅 Google Cloud 示例浏览器