How to create Pandas DataFrame from nested XML? Last Updated : 28 Apr, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to create Pandas DataFrame from nested XML. We will use the xml.etree.ElementTree module, which is a built-in module in Python for parsing or reading information from the XML file. The ElementTree represents the XML document as a tree and the Element represents only a single node of the tree. Functions Used: Here, we will use some functions to process out code which is stated below: ElementTree.parse( XML_file) : To read data from an XML fileroot.iter('root_name'): To iterate through the branches of the root nodeElementTree.fromstring(XML_file) : To read data when XML code which is passed as a string inside triple quotes in the python codeprstree.findall('store'): To find all the elements of the parsed XML ElementTreenode.attribute.get(attribte_name ): To get the attributenode.find(attribte_name): To retrieve the text content of the mentioned attribute_namepandas.DataFrame() : To convert the XML data to a DataFramelist.append(): To append the items to a listApproachParse or read the XML file using ElementTree.parse( ) function and get the root element.Iterate through the root node to get the child nodes attributes 'SL NO' (here) and extract the text values of each attribute (here foodItem, price, quantity, and discount).Get the respective food items with specifications as a unit appended to a list(here all_items() list).Convert the list into a DataFrame using pandas.DataFrame() function and mention the column names within quotes separated by commas.Print the DataFrame and it's done.Input Nested XML Data XML <?xml version="1.0" encoding="UTF-8"?> <Food> <Info> <Msg>Food Store items.</Msg> </Info> <store slNo="1"> <foodItem>meat</foodItem> <price>200</price> <quantity>1kg</quantity> <discount>7%</discount> </store> <store slNo="2"> <foodItem>fish</foodItem> <price>150</price> <quantity>1kg</quantity> <discount>5%</discount> </store> <store slNo="3"> <foodItem>egg</foodItem> <price>100</price> <quantity>50 pieces</quantity> <discount>5%</discount> </store> <store slNo="4"> <foodItem>milk</foodItem> <price>50</price> <quantity>1 litre</quantity> <discount>3%</discount> </store> </Food> Example 1: In this code below we have parsed the XML file. Give the complete path where you have saved the XML file within quotes. So here we need to use ElementTree.parse() function to read the data from the XML file and then the getroot() function to get the root. Then follow the steps given. Python3 import xml.etree.ElementTree as ETree import pandas as pd # give the path where you saved the xml file # inside the quotes xmldata = "C: \\ProgramData\\Microsoft\\ Windows\\Start Menu\\Programs\\ Anaconda3(64-bit)\\xmltopandas.xml" prstree = ETree.parse(xmldata) root = prstree.getroot() # print(root) store_items = [] all_items = [] for storeno in root.iter('store'): store_Nr = storeno.attrib.get('slNo') itemsF = storeno.find('foodItem').text price = storeno.find('price').text quan = storeno.find('quantity').text dis = storeno.find('discount').text store_items = [store_Nr, itemsF, price, quan, dis] all_items.append(store_items) xmlToDf = pd.DataFrame(all_items, columns=[ 'SL No', 'ITEM_NUMBER', 'PRICE', 'QUANTITY', 'DISCOUNT']) print(xmlToDf.to_string(index=False)) Output: Note: The XML file should be saved in the same directory or folder where your Python code saved. Example 2: We can also pass the XML content as a string inside triple quotes. In that case, we need to use the fromstring() function to read the string. Get the root using the 'tag' object and follow the same steps to convert it to a DataFrame as mentioned above. Python3 import xml.etree.ElementTree as ETree import pandas as pd xmldata = '''<?xml version="1.0" encoding="UTF-8"?> <Food> <Info> <Msg>Food Store items.</Msg> </Info> <store slNo="1"> <foodItem>meat</foodItem> <price>200</price> <quantity>1kg</quantity> <discount>7%</discount> </store> <store slNo="2"> <foodItem>fish</foodItem> <price>150</price> <quantity>1kg</quantity> <discount>5%</discount> </store> <store slNo="3"> <foodItem>egg</foodItem> <price>100</price> <quantity>50 pieces</quantity> <discount>5%</discount> </store> <store slNo="4"> <foodItem>milk</foodItem> <price>50</price> <quantity>1 litre</quantity> <discount>3%</discount> </store> </Food> ''' prstree = ETree.fromstring(xmldata) root = prstree.tag #print(root) store_items = [] all_items = [] for storeno in prstree.findall('store'): store_Nr = storeno.attrib.get('slNo') itemsF= storeno.find('foodItem').text price= storeno.find('price').text quan= storeno.find('quantity').text dis= storeno.find('discount').text store_items = [store_Nr,itemsF,price,quan,dis] all_items.append(store_items) xmlToDf = pd.DataFrame(all_items,columns=[ 'SL No','ITEM_NUMBER','PRICE','QUANTITY','DISCOUNT']) print(xmlToDf.to_string(index=False)) Output: Comment More infoAdvertise with us Next Article How to create Pandas DataFrame from nested XML? rijushree100guha Follow Improve Article Tags : Python Python-pandas Python pandas-dataFrame Practice Tags : python Similar Reads How To Convert Pandas Dataframe To Nested Dictionary In this article, we will learn how to convert Pandas DataFrame to Nested Dictionary. Convert Pandas Dataframe To Nested DictionaryConverting a Pandas DataFrame to a nested dictionary involves organizing the data in a hierarchical structure based on specific columns. In Python's Pandas library, we ca 2 min read Create a Pandas DataFrame from Lists Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example# Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diffe 5 min read How to create DataFrame from dictionary in Python-Pandas? The task of converting a dictionary into a Pandas DataFrame involves transforming a dictionary into a structured, tabular format where keys represent column names or row indexes and values represent the corresponding data.Using Default ConstructorThis is the simplest method where a dictionary is dir 3 min read Create a list from rows in Pandas DataFrame | Set 2 In an earlier post, we had discussed some approaches to extract the rows of the dataframe as a Python's list. In this post, we will see some more methods to achieve that goal. Note : For link to the CSV file used in the code, click here. Solution #1: In order to access the data of each row of the Pa 2 min read Different ways to create Pandas Dataframe It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python.Example: Creating a DataFrame from a DictionaryPythonimport pandas as pd # initialize data of lists. data = {'Name': ['Tom', 7 min read How to add column from another DataFrame in Pandas ? In this discussion, we will explore the process of adding a column from another data frame in Pandas. Pandas is a powerful data manipulation library for Python, offering versatile tools for handling and analyzing structured data. Add column from another DataFrame in Pandas There are various ways to 6 min read How to Convert Pandas DataFrame into a List? In this article, we will explore the process of converting a Pandas DataFrame into a List, We'll delve into the methods and techniques involved in this conversion, shedding light on the versatility and capabilities of Pandas for handling data structures in Python.Ways to convert Pandas DataFrame Int 7 min read How to load a TSV file into a Pandas DataFrame? In this article, we will discuss how to load a TSV file into a Pandas Dataframe. The idea is extremely simple we only have to first import all the required libraries and then load the data set by using various methods in Python. Dataset Used:  data.tsv Using read_csv() to load a TSV file into a Pan 1 min read Python Pandas Dataframe To Nested Json When working with data in Python,Pandas is a popular library for handling tabular data efficiently. Converting a Pandas DataFrame to a nested JSON structure can be necessary for various reasons, such as preparing data for API responses or interacting with nested JSON-based data structures. In this a 3 min read Create a Pandas DataFrame from List of Dicts Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used Pandas object. Pandas DataFrame can be created in multiple ways using Python. Letâs discuss how to create a Pandas DataFrame from the List of Dictionaries. C 3 min read Like