I have to fetch the data from multiple tables separately from BigQuery into same dataframe using python in local laptop
Sure, you can use an f-string here to just insert so to say your table name into the query
import os
import pandas as pd
from google.cloud import bigquery
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="C:/Users/GA/credentials.json"
client = bigquery.Client()
dflist = []
QUERY = (
"""
SELECT
fullVisitorId,
visitNumber,
visitId,
visitStartTime,
date,
totals.bounces,
totals.hits,
totals.newVisits,
totals.pageviews,
totals.screenviews,
totals.sessionQualityDim,
totals.timeOnScreen,
totals.timeOnSite,
totals.totalTransactionRevenue,
totals.transactionRevenue,
totals.transactions
FROM
bigquery-public-data:google_analytics_sample.ga_sessions_{date}
order by fullVisitorId, date, visitNumber, hitNumber
"""
)
def query_data(date_:str) -> pd.DataFrame:
query_job = client.query(QUERY.format(date=date_))
query_result = query_job.result()
return query_result.to_dataframe()
dates = ["2021", "2022", ...]
finaldf = pd.concat((query_data(date) for date in dates),axis = 0)
note that you don't have to explicitly pass the client and the query to the function as it can just use them from the surrounding scope.