I am trying to do an XML export of a BigQuery query via python using a code I found at this link: Python Pandas Dataframe to XML but I have the error below, could you help me solve this?
import os
import pandas as pd
from datetime import datetime, timedelta
from google.cloud import bigquery
script_path = "C:\\Keys\\key.json"
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = script_path
client = bigquery.Client(project='project_id')
query_job = client.query("SELECT field1, field2, field3 FROM dataset.table ")
df_bq = query_job.to_dataframe()
def to_xml(df, filename=None, mode='w'):
def row_to_xml(row):
xml = ['<item>']
for i, col_name in enumerate(row.index):
xml.append(' <field name="{0}">{1}</field>'.format(col_name, row.iloc[i]))
xml.append('</item>')
return '\n'.join(xml)
res = '\n'.join(df.apply(row_to_xml, axis=1))
if filename is None:
return res
with open(filename, mode) as f:
f.write(res)
pd.DataFrame.to_xml = to_xml
df.to_xml('test.xml')
NameError Traceback (most recent call last) in ----> 1 df.to_xml('table.xml')
NameError: name 'df' is not defined