Python XML file to pandas dataframe

0 votes
How to convert an xml file to pandas dataframe?
Aug 1, 2019 in Python by Rishi
8,990 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, 2019 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,560 points
34,445 views
0 votes
1 answer

How to create Pandas dataframe from Python list?

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

answered Apr 6, 2019 in Python by Likhita
1,275 views
+1 vote
3 answers

How to change/update cell value in Python Pandas dataframe?

You can use the at() method to ...READ MORE

answered Apr 8, 2019 in Python by Kunal
147,342 views
0 votes
1 answer
0 votes
1 answer

How to rename columns in pandas (Python)?

You can use the rename function in ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by MD 2,049 views
0 votes
1 answer

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

The major difference is "size" includes NaN values, ...READ MORE

answered Apr 30, 2018 in Data Analytics by DeepCoder786
• 1,720 points

edited Jun 8, 2020 by Gitika 2,850 views
0 votes
2 answers

Replacing a row in pandas data.frame

key error. I love python READ MORE

answered Feb 18, 2019 in Data Analytics by anonymous
13,737 views
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,660 points
4,571 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 aryya
• 7,460 points
1,185 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
• 58,020 points
11,710 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP