Convert XML to DataFrame in Python using pandas - Part #2

Published: 08 February 2021
on channel: Adimulam Natarajan
5,678
46

This demo explains everything you need to successfully apply the steps in your project

setup on windows:

python -m pip install -U pip setuptools
pip3 install jupyter
jupyter notebook

ModuleNotFoundError: No module named 'requests'
pip install requests

ModuleNotFoundError: No module named 'pandas'
pip install pandas

1. Imports

import requests
import xml.etree.ElementTree as ET
import pandas as pd

2. get XML

user_agent_url = 'http://www.user-agents.org/allagents....
xml_data = requests.get(user_agent_url).content

3. define function

def xml2df(xml_data):
root = ET.XML(xml_data) # element tree
all_records = []
for i, child in enumerate(root):
record = {}
for subchild in child:
record[subchild.tag] = subchild.text
all_records.append(record)
return pd.DataFrame(all_records)

4. Test

df = xml2df(xml_data)
df.shape
df.head()

Part #1

   • Convert XML to DataFrame in Python using p...  


On this page of the site you can watch the video online Convert XML to DataFrame in Python using pandas - Part #2 with a duration of hours minute second in good quality, which was uploaded by the user Adimulam Natarajan 08 February 2021, share the link with friends and acquaintances, this video has already been watched 5,678 times on youtube and it was liked by 46 viewers. Enjoy your viewing!