Python: XML file to pandas dataframe

0 votes
How to convert an xml file to pandas dataframe?
Aug 1 in Python by Rishi
38 views

1 answer to this question.

0 votes

Here's an example code:

import pandas as pd 

import xml.etree.ElementTree as et 

    

xtree = et.parse("student.xml")

xroot = xtree.getroot() 


df_cols = ["name", "email", "grade", "age"]

out_df = pd.DataFrame(columns = df_cols)


for node in xroot: 

    s_name = node.attrib.get("name")

    s_mail = node.find("email").text if node is not None else None

    s_grade = node.find("grade").text if node is not None else None

    s_age = node.find("age").text if node is not None else None

    

    out_df = out_df.append(pd.Series([s_name, s_mail, s_grade, s_age],

                                     index = df_cols), 

                           ignore_index = True)

1.In the above code we have imported pandas and ElementTree,

 ElementTree breaks the xml document into a tree structure which is easy to work with

 2.We have parsed or extracted the xml file and stored in xtree,

 Every part of a tree (root included) has a tag that describes the element.

 3.df_clos has the coloumn names which is in xml and which we want to store in dataframe

   out_df here all the coloumns are stored in a dataframe

4. A for loop to extract all the data and we are storing the data in the variable i,e s_name,s_mail etc,

    here find() finds the first child with a particular tag

5.In Out_df we are appending the data which has been converted to dataframe

answered Aug 1 by Sharon

Related Questions In Python

0 votes
1 answer

How to convert a Pandas GroupBy object to DataFrame in Python

g1 here is a DataFrame. It has a hierarchical index, ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,520 points
4,699 views
0 votes
1 answer

How to create Pandas dataframe from Python list?

You can do it like this: import ...READ MORE

answered Apr 5 in Python by Likhita
44 views
0 votes
1 answer
0 votes
1 answer

How to rename columns in pandas (Python)?

It is easy by just adding ".columns" ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,700 points
88 views
0 votes
1 answer

What is the Difference in Size and Count in pandas (python)?

The major difference is size includes NaN ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,700 points
679 views
0 votes
2 answers
0 votes
1 answer

Converting a pandas data-frame to a dictionary

Emp_dict=Employee.to_dict('records') You can directly use the 'to_dict()' function ...READ MORE

answered May 23, 2018 in Data Analytics by Bharani
• 4,550 points
1,178 views
0 votes
1 answer

Python using basicConfig method to log to console and file

I can't reproduce it on Python 3.3. ...READ MORE

answered Aug 14, 2018 in Python by ariaholic
• 7,340 points
38 views
0 votes
1 answer

How to replace values with None in Pandas data frame in Python?

Actually in later versions of pandas this ...READ MORE

answered Aug 30, 2018 in Python by Priyaj
• 56,520 points
927 views