MustardRecord
MustardRecord

Reputation: 305

SQL query results to Pandas Dataframe

I have the results from a query obtained through Python:

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

I'd like to convert them to a pandas Dataframe.

I'm trying things like df = pd.read_sql_query(query, connection), but I don't have the connection.

My code to get the query results is

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()

Upvotes: 0

Views: 3248

Answers (1)

robinood
robinood

Reputation: 1248

You can use the pandas-gbq package and use the read_gbq function as suggested in this post.

The read_gbq function takes the SQL string as argument and the different credentials for the authentication.

Upvotes: 2

Related Questions