title: Making XML to Dataframe for Pandas Python
body: <p>I want to convert a XML-file to a Dataframe so I can do Data Analysis trough Pandas, in my Python notebook. However all the solutions I find on this website give me errors. </p>
  
  <p>The following is a short version of the XML:</p>
  
  <pre><code>    &lt;EML xmlns="urn:oasis:names:tc:evs:schema:eml" xmlns:ns2="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0" xmlns:ns3="urn:oasis:names:tc:ciq:xsdschema:xNL:2.0" xmlns:ns4="http://www.w3.org/2000/09/xmldsig#" xmlns:ns5="urn:oasis:names:tc:evs:schema:eml:ts" xmlns:ns6="http://www.kiesraad.nl/extensions" xmlns:ns7="http://www.kiesraad.nl/reportgenerator" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Id="230b" SchemaVersion="5" xsi:schemaLocation="urn:oasis:names:tc:evs:schema:eml 230-candidatelist-v5-0.xsd http://www.kiesraad.nl/extensions kiesraad-eml-extensions.xsd"&gt;
  &lt;!--
  Created by: Ondersteunende Software Verkiezingen by IVU Traffic Technologies AG, program: P2-3, version: 2.19.2
  --&gt;
  &lt;TransactionId&gt;1&lt;/TransactionId&gt;
  &lt;ManagingAuthority&gt;
  &lt;AuthorityIdentifier Id="CSB"&gt;De Kiesraad&lt;/AuthorityIdentifier&gt;
  &lt;AuthorityAddress/&gt;
  &lt;/ManagingAuthority&gt;
  &lt;IssueDate&gt;2017-02-13&lt;/IssueDate&gt;
  &lt;ns6:CreationDateTime&gt;2017-02-13T16:35:14.403+01:00&lt;/ns6:CreationDateTime&gt;
  &lt;ns4:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments"/&gt;
  &lt;CandidateList&gt;
  &lt;Election&gt;
  &lt;ElectionIdentifier Id="TK2017"&gt;
  &lt;ElectionName&gt;Tweede Kamer der Staten-Generaal 2017&lt;/ElectionName&gt;
  &lt;ElectionCategory&gt;TK&lt;/ElectionCategory&gt;
  &lt;ns6:ElectionSubcategory&gt;TK&lt;/ns6:ElectionSubcategory&gt;
  &lt;ns6:ElectionDate&gt;2017-03-15&lt;/ns6:ElectionDate&gt;
  &lt;ns6:NominationDate&gt;2017-01-30&lt;/ns6:NominationDate&gt;
  &lt;/ElectionIdentifier&gt;
  &lt;Contest&gt;
  &lt;ContestIdentifier Id="9"&gt;
  &lt;ContestName&gt;Amsterdam&lt;/ContestName&gt;
  &lt;/ContestIdentifier&gt;
  &lt;Affiliation&gt;
  &lt;AffiliationIdentifier Id="1"&gt;
  &lt;RegisteredName&gt;VVD&lt;/RegisteredName&gt;
  &lt;/AffiliationIdentifier&gt;
  &lt;Type&gt;stel gelijkluidende lijsten&lt;/Type&gt;
  &lt;ns6:ListData BelongsToSet="1" PublicationLanguage="nl" PublishGender="true"/&gt;
  &lt;Candidate&gt;
  &lt;CandidateIdentifier Id="1"/&gt;
  &lt;CandidateFullName&gt;
  &lt;ns3:PersonName&gt;
  &lt;ns3:NameLine NameType="Initials"&gt;M.&lt;/ns3:NameLine&gt;
  &lt;ns3:FirstName&gt;Mark&lt;/ns3:FirstName&gt;
  &lt;ns3:LastName&gt;Rutte&lt;/ns3:LastName&gt;
  &lt;/ns3:PersonName&gt;
  &lt;/CandidateFullName&gt;
  &lt;Gender&gt;male&lt;/Gender&gt;
  &lt;QualifyingAddress&gt;
  &lt;ns2:Locality&gt;
  &lt;ns2:LocalityName&gt;'s-Gravenhage&lt;/ns2:LocalityName&gt;
  &lt;/ns2:Locality&gt;
  &lt;/QualifyingAddress&gt;
  &lt;/Candidate&gt;
  &lt;Candidate&gt;
  &lt;CandidateIdentifier Id="2"/&gt;
  &lt;CandidateFullName&gt;
  &lt;ns3:PersonName&gt;
  &lt;ns3:NameLine NameType="Initials"&gt;J.A.&lt;/ns3:NameLine&gt;
  &lt;ns3:FirstName&gt;Jeanine&lt;/ns3:FirstName&gt;
  &lt;ns3:LastName&gt;Hennis-Plasschaert&lt;/ns3:LastName&gt;
  &lt;/ns3:PersonName&gt;
  &lt;/CandidateFullName&gt;
  &lt;Gender&gt;female&lt;/Gender&gt;
  &lt;QualifyingAddress&gt;
  &lt;ns2:Locality&gt;
  &lt;ns2:LocalityName&gt;Nederhorst den Berg&lt;/ns2:LocalityName&gt;
  &lt;/ns2:Locality&gt;
  &lt;/QualifyingAddress&gt;
  &lt;/Candidate&gt;
  &lt;/Affiliation&gt;
  &lt;/Contest&gt;
  &lt;/Election&gt;
  &lt;/CandidateList&gt;
  &lt;/EML&gt;
  </code></pre>
  
  <p>I want to adjust it through Python commands and want to do it al in my Kernel, and then I want to be able to do my Pandas on this. </p>
  
  <p>Datasource:
  <a href="https://data.openstate.eu/dataset/kandidatenlijsten" rel="nofollow noreferrer">https://data.openstate.eu/dataset/kandidatenlijsten</a>
  This is the code I am using now:
  <a href="http://www.austintaylor.io/lxml/python/pandas/xml/dataframe/2016/07/08/convert-xml-to-pandas-dataframe/" rel="nofollow noreferrer">http://www.austintaylor.io/lxml/python/pandas/xml/dataframe/2016/07/08/convert-xml-to-pandas-dataframe/</a>:`</p>
  
  <pre><code>import xml.etree.ElementTree as ET
  from lxml import etree
  import pandas as pd
  
  xml_data = 'Kandidatenlijsten_TK2017_Amsterdam.eml'
  
  def xml2df(xml_data):
      tree = ET.parse(xml_data)
      root = tree.getroot()
      all_records = []
      headers = []
      for i, child in enumerate(root):
          record = []
          for subchild in child:
              record.append(subchild.text)
              if subchild.tag not in headers:
                  headers.append(subchild.tag)
          all_records.append(record)
      return pd.DataFrame(all_records, columns=headers)`
  </code></pre>
  
  <p>This gives the error:<code>AssertionError: 3 columns passed, passed data had 2 columns</code></p>
  
  <p>Thank you Kindly, Kind regards.</p>