0

I am working on putting my Google BigQuery data into a pandas dataframe. I am successfully able to run the below code and print the result set.

from google.cloud import bigquery
from google.oauth2 import service_account
from pandas.io import gbq

credentials = service_account.Credentials.from_service_account_file('My Project Credentials.json')
project_id = 'essential-cairn-253818'
client = bigquery.Client(credentials= credentials,project=project_id)
query = client.query("""
SELECT  device.model as model
FROM     `my-table-name`
LIMIT 100
""")

results = query.result()
for row in results:
  print("{}".format(row.model))

However, I would like to use the pandas.io.gbq.read_gbq() functionality to put this into a dataframe. I add the next rows of code and I get stuck.

query2 = """
SELECT  device.model as model
FROM     `my-table-name`
LIMIT 100
"""
results_df = gbq.read_gbq(query2, project_id=project_id, private_key='My Project Credentials.json', dialect = 'standard')

This produces the error:

TypeError: 'RowIterator' object is not callable

I'm not sure where I'm going wrong. I am following the question seen here: Live data from BigQuery into a Python DataFrame

Can anyone point me in the right direction?

1
  • Consider using the pandas-gbq package. pandas-gbq.readthedocs.io/en/latest . Your other option is loop through results (as you have done) and append each row to a list, then convert the list to a pandas df Commented Sep 24, 2019 at 20:48

3 Answers 3

0

I'm new too.

You could try pandas.read_gbq, seems a better choice. Documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_gbq.html

Also this other method seems less problematic: How to integrate Bigquery & pandas

Sign up to request clarification or add additional context in comments.

Comments

0

This was fixed with closing Spyder and restarting Python... something was going on unrelated to the code in this query. Very odd, but working now!

Comments

0

Why don't you use the official method query_job.result().to_dataframe()?

My code is:

results = query_job.result()
df = results.to_dataframe()
df.to_csv('demo1.csv',index=False, encoding='utf-8', sep = ',')

2 Comments

I recommend against answers in the shape of a rhetoric question, it risks being misunderstood as a question where an answer should be.
query_job.to_dataframe() without the .results() part seems to give the same output as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.