Python create XML file from DataFrame

Chapter: Python Pandas Last Updated: 19-03-2023 17:31:09 UTC


            /* ............... START ............... */
import pandas as pd
import xml.etree.ElementTree as ET

# Create a sample DataFrame
data = {
    'Name': ['John', 'Mary', 'Mike'],
    'Age': [25, 30, 35],
    'Gender': ['Male', 'Female', 'Male']
df = pd.DataFrame(data)

# Create the root element of the XML file
root = ET.Element('Data')

# Loop through each row in the DataFrame and create a new element for each row
for i, row in df.iterrows():
    # Create a new element with the row index as the tag name
    row_element = ET.SubElement(root, f"Row_{i}")
    # Add child elements for each column in the row
    for col_name, col_value in row.items():
        col_element = ET.SubElement(row_element, col_name)
        col_element.text = str(col_value)

# Create an ElementTree object and write it to a file
tree = ET.ElementTree(root)
tree.write('data.xml', encoding='utf-8', xml_declaration=True)

                /* ............... END ............... */


program would create an XML file named data.xml with the following content:

<?xml version='1.0' encoding='utf-8'?>


  • The program demonstrates how to create an XML file from a pandas DataFrame using the xml.etree.ElementTree module in Python.
  • To start with, the program defines a sample DataFrame with some data, containing information about people's names, ages, and genders.
  • Next, it creates the root element of the XML file using the Element function from the xml.etree.ElementTree module. The root element is assigned the tag name "Data".
  • The program then iterates through each row in the DataFrame using the iterrows() method. For each row, it creates a new element using the SubElement function, which is added to the root element as a child element. The tag name of the row element is based on the row index of the DataFrame.
  • Inside each row element, the program adds child elements for each column of the DataFrame. The tag names of these child elements correspond to the column names, and their text content is set to the values in the DataFrame.
  • Finally, the program creates an ElementTree object from the root element and writes it to a file using the write method. The file is saved with the name "data.xml" and includes an XML declaration that specifies the version and encoding of the file.
  • Overall, the program demonstrates how to easily convert a pandas DataFrame to an XML file using the xml.etree.ElementTree module.
Similar Programs Chapter Last Updated
Import Pandas Python Python Pandas 26-04-2021