Advertisement
ipetkov

XML Loader to DataFrame

Mar 16th, 2025
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import os
  2. import glob
  3. import xml.etree.ElementTree as ET
  4. import pandas as pd
  5.  
  6.  
  7.  
  8.  
  9. current_dir = os.getcwd()
  10. directory = os.path.join(current_dir, "data")
  11.  
  12. xml_files = glob.glob(os.path.join(directory, "*.xml"))
  13.  
  14. data = pd.DataFrame()
  15.  
  16. # Обработваме всички XML файлове и добавяме данни директно в DataFrame
  17. for file in xml_files:
  18.     tree = ET.parse(file)
  19.     xml_root = tree.getroot()
  20.  
  21.     # Извличаме данни в DataFrame
  22.     for child in xml_root:
  23.         item = {elem.tag: elem.text for elem in child}  # dict comprehension
  24.         # Добавяме текущия ред към DataFrame
  25.         data = pd.concat([data, pd.DataFrame([item])], ignore_index=True)
  26.  
  27. data
  28.  
  29. #Check type
  30. print(data.dtypes)
  31.  
  32. # Change type
  33. data['price'] = pd.to_numeric(data['price'], errors='coerce')
  34. data['quantity'] = pd.to_numeric(data['quantity'], errors='coerce')
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement