user7289 user7289 - 4 years ago 608
Python Question

How do convert a pandas/dataframe to XML?

is there a simple way to take a pandas/df table:

field_1 field_2 field_3 field_4
cat 15,263 2.52 00:03:00
dog 1,652 3.71 00:03:47
test 312 3.27 00:03:41
book 300 3.46 00:02:40


And convert it to XML along the lines of:

<item>
<field name="field_1">cat</field>
<field name="field_2">15263</field>
<field name="filed_3">2.52</field>

...

<item>
<field name="field_1">dog</field>

and so on...


Thanks in advance for any help.

Answer Source

You can create a function that creates the item node from a row in your DataFrame:

def func(row):
    xml = ['<item>']
    for field in row.index:
        xml.append('  <field name="{0}">{1}</field>'.format(field, row[field]))
    xml.append('</item>')
    return '\n'.join(xml)

And then apply the function along the axis=1.

>>> print '\n'.join(df.apply(func, axis=1))
<item>
  <field name="field_1">cat</field>
  <field name="field_2">15,263</field>
  <field name="field_3">2.52</field>
  <field name="field_4">00:03:00</field>
</item>
<item>
  <field name="field_1">dog</field>
  <field name="field_2">1,652</field>
  <field name="field_3">3.71</field>
  <field name="field_4">00:03:47</field>
</item>
...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download