Syntax:
Let’s see the syntax and parameters that are passed to pandas.DataFrame.to_xml() function:
- 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.
- 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.
- 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.
- 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.
- na_rep (by default = None): It represents the missing values.
- attr_cols (by default = None): We can specify the list of columns to write as attributes in the row element of the XML document.
- namespaces (by default = None): Custom namespaces are provided to this parameter. They are defined in the root element.
- prefix (by default = None): The prefix is specified for each and every element and attribute in the XML document.
- encoding (by default = “utf-8”): We can specify the resulting XML encoding with this parameter.
- xml_declaration (by default = True): XML declaration can be excluded in the resulting XML document if it is set to “False”.
- 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.
# 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”.
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.
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”.
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”.
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”.
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”.
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”.
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”.
print(Temperature.to_xml(pretty_print=False))
Output:
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.
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.