Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Read XML file to Pandas DataFrame

from lxml import objectify
xml = objectify.parse('Document1.xml')
root = xml.getroot()

data=[]
for i in range(len(root.getchildren())):
    data.append([child.text for child in root.getchildren()[i].getchildren()])

df = pd.DataFrame(data).T
df.columns = ['bathrooms', 'price', 'property_id']
Comment

Python: Extracting XML to DataFrame (Pandas)

path = 'AttributesXMLPandas.xml'
dfcols = ['ID', 'Text', 'CreationDate']

root = et.parse(path)
rows = root.findall('.//row')

# NESTED LIST
xml_data = [[row.get('Id'), row.get('Text'), row.get('CreationDate')] 
            for row in rows]

df_xml = pd.DataFrame(xml_data, columns=dfcols)

print(df_xml)

#   ID   Text             CreationDate
# 0  1  (...)  2011-08-30T21:15:28.063
# 1  2  (...)  2011-08-30T21:24:56.573
# 2  3  (...)                     None
Comment

PREVIOUS NEXT
Code Example
Python :: ascii to decimal python 
Python :: python set comparison 
Python :: rename key in dict python 
Python :: python generate id 
Python :: find record where dataframe column value contains 
Python :: how to enable execution time in jupyter lab 
Python :: python optionmenu tkinter 
Python :: save dataframe to a csv local file pyspark 
Python :: check if part of list is in another list python 
Python :: python [remote rejected] master - master (pre-receive hook declined) 
Python :: python remove nan rows 
Python :: python delete white spaces 
Python :: unix command in python script 
Python :: flatten numpy array 
Python :: erase % sign in row pandas 
Python :: python csv to list 
Python :: calcutalte average python 
Python :: run python file using python code 
Python :: how to fill a list in python 
Python :: arch linux python 3.7 
Python :: Ask a user for input python 
Python :: runge kutta 
Python :: own labels for ticks matplotlib 
Python :: enumerate vs zip python same time 
Python :: increase colorbar ticksize 
Python :: python list to string without brackets 
Python :: mutable and immutable in python 
Python :: power level in google colab 
Python :: flask return error response 
Python :: python turtle write 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =