How to Create an XML File from a Pandas DataFrame in Python
You can use the pandas.DataFrame.to_xml()
method to write a DataFrame to an XML file. This method takes several arguments, such as the file path, the element name for rows, and whether or not to include the DataFrame’s index. I have an another article How to Create a Pandas DataFrame in Python, where you will learn how to create a Pandas DataFrame in Python.
For this tutorial, I will use a sample DataFrame as below:
id name math_score 0 202301 Minhaj 92 1 202302 Ridhwan 86 2 202303 Tanveer 76 3 202304 Sharodia 89 4 202305 Alve 99 5 202306 Intisar 99
Here’s an example of how you might use this method to write a DataFrame to an XML file:
import pandas as pd df.to_xml('sample.xml', index=False, element_name='row')
In this tutorial, I tried to brief how to create an XML file from a Pandas DataFrame in Python. Hope you have enjoyed the tutorial. If you want to get updated, like my facebook page https://www.facebook.com/LearningBigDataAnalytics and stay connected.