title: Extracting XML into data frame with parent attribute as column title
body: <p>I have thousands of XML files that I will be processing, and they have a similar format, but different parent names and different numbers of parents. 
  Through books, google, tutorials, and just trying out codes, I've been able to pull out all of this data. See, for example: <a href="https://stackoverflow.com/questions/16922432/parsing-xml-to-pandas-data-frame-throws-memory-error">Parsing xml to pandas data frame throws memory error</a> and <a href="https://stackoverflow.com/questions/16943417/dynamic-search-through-xml-attributes-using-lxml-and-xpath-in-python?lq=1">Dynamic search through xml attributes using lxml and xpath in python</a></p>
  
  <p>However, I realized that I was extracting the data poorly, with a child "Time" repeated for each parent. </p>
  
  <p>Here is what I am trying to get. </p>
  
  <pre><code>Time   blah   abc
  1200   100   2
  1300   30    4
  1400   70    2
  </code></pre>
  
  <p>Here is what I know how to get. But my current method is clunky (I'll show below the example XML)</p>
  
  <pre><code>    child      Time   grandchild
  0     blah     1200    100
  1     blah     1300    30
  ...
  n-2   abc      1200    2
  n-1   abc      1300    4
  n     abc      1400    2
  </code></pre>
  
  <p>Example XML format</p>
  
  <pre><code>&lt;outer&gt;
     &lt;inner&gt;
        &lt;parent name = "blah" id = "1"&gt; 
           &lt;child Time = "1200"&gt; 
              &lt;grandchild&gt;100&lt;/grandchild&gt;  
           &lt;/child&gt;
           &lt;child Time = "1300"&gt;
              &lt;grandchild&gt;30&lt;/grandchild&gt;
           &lt;/child&gt;
           &lt;child Time = "1400"&gt;
              &lt;grandchild&gt;70&lt;/grandchild&gt;
           &lt;/child&gt;
        &lt;/parent&gt;
        &lt;parent name = "abc" id = "2"&gt; 
           &lt;child Time = "1200"&gt;   
              &lt;grandchild&gt;2&lt;/grandchild&gt; 
           &lt;/child&gt;
           &lt;child Time = "1300"&gt;
              &lt;grandchild&gt;4&lt;/grandchild&gt;
           &lt;/child&gt;
           &lt;child Time = "1400"&gt;
              &lt;grandchild&gt;2&lt;/grandchild&gt;
           &lt;/child&gt;
        &lt;/parent&gt;      
        &lt;parent name = "1234" id = "7734"&gt; 
           &lt;other&gt; 12 &lt;/other&gt;
        &lt;/parent&gt; 
     &lt;/inner&gt;
  &lt;/outer&gt;
  </code></pre>
  
  <p>Here is how I can get my output: </p>
  
  <pre><code>from lxml import etree, objectify
  from pandas import *
  dTime=[]
  dparent = []
  dgrandchild=[]
  for df in root.xpath('/*/*/*/parent/child'):
      dparent.append(df.getparent().attrib['name'])
      ## Iterate over attributes of time for specific parent
      for attrib in df.attrib:
      dTime.append(df.attrib[attrib])
          ## grandchild is a child of time, and iterate
          subfields = df.getchildren()
          for subfield in subfields:
           dgrandchild.append(subfield.text)
  df=DataFrame({'Parent': dparent,'Time':dTime,'grandchild':dgrandchld})
  </code></pre>
  
  <p>I could just take this output and re-shape it, but that seems inefficient and a very clunky approach. </p>
  
  <p>I think I need something of the flavor:</p>
  
  <pre><code>#this does not work
  data = []
  for elem in root.xpath('/*/*/*/parent/child'):
     elem_data = {}
     for attrib in elem.attrib:
         elem_data['Time'] = elem.attrib[attrib])
     for child in elem.getchildren():
         elem_data[getparent().attrib['name'])] = child.text
         data.append(elem_data)
  ndata = DataFrame(data)
  </code></pre>