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

Publicado el: 08 febrero 2021
en el canal de: 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...  


En esta página del sitio puede ver el video en línea Convert XML to DataFrame in Python using pandas - Part #2 de Duración hora minuto segunda en buena calidad , que subió el usuario Adimulam Natarajan 08 febrero 2021, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 5,678 veces y le gustó 46 a los espectadores. Disfruta viendo!