【问题标题】:SQL query results to Pandas Dataframe对 Pandas Dataframe 的 SQL 查询结果
【发布时间】:2023-04-11 02:24:01
【问题描述】:

我有一个通过 Python 获得的查询的结果:

<google.cloud.bigquery.table.RowIterator object at 0x00000289E881C670>

我想将它们转换为 pandas 数据框。

我正在尝试df = pd.read_sql_query(query, connection) 之类的东西,但我没有connection

我获取查询结果的代码是

import pandas as pd
from google.cloud import bigquery
from google.oauth2 import service_account

# Load BigQuery credentials
credentials_json_file = r"credentials.json"
credentials = service_account.Credentials.from_service_account_file(credentials_json_file)
project_id = "idi-rewan"
client = bigquery.Client(credentials=credentials, project=project_id)

# Define query
query_string = """
   SELECT * FROM `database.table`
   """
query_job = client.query(query_string)

results = query_job.result()

【问题讨论】:

    标签: python sql pandas dataframe


    【解决方案1】:

    您可以使用 pandas-gbq 包并按照this 帖子中的建议使用 read_gbq 函数。

    read_gbq 函数将 SQL 字符串作为参数,并使用不同的凭据进行身份验证。

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2021-05-26
      • 2015-10-28
      • 1970-01-01
      • 2015-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多