Python Pandas

Pandas.DataFrame.To_XML()

The pandas.DataFrame.to_xml() creates XML from the Pandas DataFrame.  Basically, this function renders the existing DataFrame to an XML document. By passing different parameters, we can create our own root tag, row name, namespaces, XML prefix, etc. All these are discussed in the Syntax section.

Syntax:

Let’s see the syntax and parameters that are passed to pandas.DataFrame.to_xml() function:

pandas.DataFrame.to_xml(path/buffer, index, root_name, row_name, na_rep, attr_cols, elem_cols, namespaces, prefix, encoding, xml_declaration, pretty_print, parser, stylesheet, compression, storage_options)
  1. path/buffer: We can specify the file name with an “.xml” extension such that the XML is stored in the specified location with the given file name. If it is not provided, it is stored and displayed as a string. In this entire guide, we display it as a string.
  2. index (by default = True): We can exclude the index in the XML document by setting it to “False”. By default, the index is included in the XML document.
  3. root_name (by default = “data”): The name of the root element can be specified with this parameter. It takes the “data” root name by default.
  4. row_name (by default = “row”): The name of the row element (child tags) can be specified with this parameter. It takes the “row” root name by default.
  5. na_rep (by default = None): It represents the missing values.
  6. attr_cols (by default = None): We can specify the list of columns to write as attributes in the row element of the XML document.
  7. namespaces (by default = None): Custom namespaces are provided to this parameter. They are defined in the root element.
  8. prefix (by default = None): The prefix is specified for each and every element and attribute in the XML document.
  9. encoding (by default = “utf-8”): We can specify the resulting XML encoding with this parameter.
  10. xml_declaration (by default = True): XML declaration can be excluded in the resulting XML document if it is set to “False”.
  11. pretty_print (by default = True): An XML output is displayed with indention and line breaks by default.

DataFrame:

We use the following DataFrame in this entire guide in which the XML is created from the data that is present in this DataFrame. First, you need to execute the following Python script. After that, you can implement the following examples. Create the “Temperature” DataFrame with two columns and four rows.

import pandas

# Create DataFrame - Temperature with 2 columns and 4 rows
Temperature = pandas.DataFrame({"Country":["India","USA","Italy","Japan"],"Celsius":[32,43,32,30]})
print(Temperature)

Output:

Example 1: Write a DataFrame to the XML String and XML File

Render the previous DataFrame into an XML document like string and XML file like “Temperature.xml”.

# Write to XML String
print(Temperature.to_xml())

# Write to XML file - 'Temperature.xml'
Temperature.to_xml('Temperature.xml')

Output:

Example 2: With Namespaces and Prefix Parameters

Render the previous DataFrame into an XML document by specifying the namespaces as “temp” https://temperaturedetails.com/ and also use the “temp” prefix for each row tag and row attributes.

# with namespaces and prefix
print(Temperature.to_xml(namespaces={"temp": "https://temperaturedetails.com"},prefix="temp"))

Output:

Example 3: With Index Parameter

Render the previous DataFrame into an XML document without indices. By default, it takes the DataFrame index. Let’s set this parameter to “False”.

# without index
print(Temperature.to_xml(index=False))

Output:

 

The “Temperature” DataFrame is rendered without indices.

Example 4: With the Root_Name Parameter

By default, an XML document is generated with a “data” root tag. Let’s render the “Temperature” DataFrame into XML by specifying the root_name as “temperatureData”.

# root_name parameter
print(Temperature.to_xml(root_name='temperatureData'))

Output:

The root tag is <temperatureData>….</temperatureData>.

Example 5: With the Row_Name Parameter

By default, an XML document is generated with a “row” row tag. Let’s render the “Temperature” DataFrame into XML by specifying the row_name as “Country”.

# row_name parameter
print(Temperature.to_xml(row_name='Country'))

Output:

The row tag is <country>….</country>.

Example 6: With Encoding Parameter

By default, an XML document is generated with encoding “utf-8”. Let’s specify the encoding as “utf-16”.

# encoding parameter
print(Temperature.to_xml(encoding='utf-16'))

Output:

Example 7: With Xml_Declaration Parameter

By default, an XML document is generated with the xml declaration. Let’s generate XML without the declaration by setting the xml_declaration parameter to “False”.

#  xml_declaration parameter
print(Temperature.to_xml(xml_declaration=False)

Output:

Example 8: With Pretty_Print Parameter

XML is pretty printed by default (line break and indention). Let’s set the pretty_print parameter to “False”.

# pretty_print parameter
print(Temperature.to_xml(pretty_print=False))

Output: 

<?xml version='1.0' encoding='utf-8'?> <data><row><index>0</index><Country>India</Country><Celsius>32</Celsius></row><row><index>1</index><Country>USA</Country><Celsius>43</Celsius></row><row><index>2</index><Country>Italy</Country><Celsius>32</Celsius></row><row><index>3</index><Country>Japan</Country><Celsius>30</Celsius></row></data>

It’s not pretty printed since the pretty_print parameter is set to “False”.

Example 9: With Attr_Cols Parameter

Let’s render the “Temperature” DataFrame into XML with only the “Country” column. Pass the attr_cols parameter and assign this column through a list.

# attr_cols parameter
print(Temperature.to_xml(attr_cols=['Country']))

Output:

XML is generated from the “Temperature” DataFrame with only one column which is “Country”.

Conclusion

We learned how to render the Pandas DataFrame into an XML document using the pandas.DataFrame.xml() function. All the necessary parameters were discussed as separate examples along with outputs. We can create our own root tag, row name, namespaces, and XML prefix.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain