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

Publicado em: 08 Fevereiro 2021
no 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...  


Nesta página do site você pode assistir ao vídeo on-line Convert XML to DataFrame in Python using pandas - Part #2 duração hora minuto segundo em boa qualidade , que foi baixado pelo usuário Adimulam Natarajan 08 Fevereiro 2021, compartilhe o link com seus amigos e conhecidos, no youtube este vídeo já foi visto 5,678 vezes e gostou 46 espectadores. Boa visualização!