Overview:
The read_xml() function of the pandas module reads data in XML format from a file or from a string and creates a DataFrame object with the contents of the XML.
Example:
# Example Python program that reads an xml file # XML file path # Read XML into a pandas DataFrame # Print the DataFrame contents |
Contents of the input XML file:
<?xml version='1.0' encoding='utf-8'?> <Elements> <Element> <Name>Lithium</Name> <Symbol>Li</Symbol> <ElectronCount>3</ElectronCount> <Group>Alkali Metal</Group> </Element> <Element> <Name>Sodium</Name> <Symbol>Na</Symbol> <ElectronCount>11</ElectronCount> <Group>Alkali Metal</Group> </Element> <Element> <Name>Potassium</Name> <Symbol>Ka</Symbol> <ElectronCount>19</ElectronCount> <Group>Alkali Metal</Group> </Element> <Element> <Name>Rubidium</Name> <Symbol>Rb</Symbol> <ElectronCount>37</ElectronCount> <Group>Alkali Metal</Group> </Element> <Element> <Name>Cesium</Name> <Symbol>Cs</Symbol> <ElectronCount>55</ElectronCount> <Group>Alkali Metal</Group> </Element> <Element> <Name>Francium</Name> <Symbol>Fr</Symbol> <ElectronCount>87</ElectronCount> <Group>Alkali Metal</Group> </Element> </Elements> |
Output:
XML as DataFrame: Name Symbol ElectronCount Group 0 Lithium Li 3 Alkali Metal 1 Sodium Na 11 Alkali Metal 2 Potassium Ka 19 Alkali Metal 3 Rubidium Rb 37 Alkali Metal 4 Cesium Cs 55 Alkali Metal 5 Francium Fr 87 Alkali Metal |