title: BigQuery (Dataframe) to XML
body: <p>I am trying to do an XML export of a BigQuery query via python using a code I found at this link: <a href="https://stackoverflow.com/questions/31260151/python-pandas-dataframe-to-xml">Python Pandas Dataframe to XML</a> but I have the error below, could you help me solve this?</p>
  
  <pre><code>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 = ['&lt;item&gt;']
          for i, col_name in enumerate(row.index):
              xml.append('  &lt;field name="{0}"&gt;{1}&lt;/field&gt;'.format(col_name, row.iloc[i]))
          xml.append('&lt;/item&gt;')
          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')
  </code></pre>
  
  <blockquote>
    <p>NameError                                 Traceback (most recent call
    last)  in 
    ----> 1 df.to_xml('table.xml')</p>
    
    <p>NameError: name 'df' is not defined</p>
  </blockquote>