3000 explained code solutions for 75 technologies


python-pandasHow to save dataframe to XML file


import pandas as pd

df = pd.DataFrame({
  'Phone': ['ip5', 'ip6', 'ip8', 'sms', 'xi'],
  'Price': [204, 304, 404, 405, 305]
})

df.to_xml('/path/to/file.xml');ctrl + c
import pandas as pd

load Pandas module

pd.DataFrame

creates Pandas DataFrame object

to_xml

saves dataframe to XML file

/path/to/file.xml

path to write file to


Usage example

cat /path/to/file.xml
output
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <Phone>ip5</Phone>
    <Price>204</Price>
  </row>
  <row>
    <index>1</index>
    <Phone>ip6</Phone>
    <Price>304</Price>
  </row>
  <row>
    <index>2</index>
    <Phone>ip8</Phone>
    <Price>404</Price>
  </row>
  <row>
    <index>3</index>
    <Phone>sms</Phone>
    <Price>405</Price>
  </row>
  <row>
    <index>4</index>
    <Phone>xi</Phone>
    <Price>305</Price>
  </row>
</data>