I'm attempting to read xml files using pandas read_xml, but struggling with including the parent attributes in the output. Is this possible using read_xml or do I need to use a different parser? Example xml, what I have attempted and desired output below.
import pandas as pd
xml = '''<?xml version="1.0" encoding="ISO-8859-1" ?>
<LocationList>
<LocationData LocationId="123" name="LocationName">
<ChannelData channelId="1" name="Temperature" >
<Values>
<VT t="2020-08-18T20:30:00">3.2</VT>
<VT t="2020-08-18T21:30:00">3.3</VT>
<VT t="2020-08-18T22:30:00">3.2</VT>
</Values>
</ChannelData>
<ChannelData channelId="2" name="Speed" >
<Values>
<VT t="2020-08-18T20:30:00">21.7</VT>
<VT t="2020-08-18T21:30:00">21.8</VT>
<VT t="2020-08-18T22:30:00">22.0</VT>
</Values>
</ChannelData>
</LocationData>
</LocationList>
'''
# Getting all VT values, but no parent attributes
pd.read_xml(xml, xpath='.//VT')
"""
t VT
0 2020-08-18T20:30:00 3.2
1 2020-08-18T21:30:00 3.3
2 2020-08-18T22:30:00 3.2
3 2020-08-18T20:30:00 21.7
4 2020-08-18T21:30:00 21.8
5 2020-08-18T22:30:00 22.0
"""
# Alternative to read one channel at the time
# But want to avoid opening file several times since they can be large
# For example like this, and then build the dataframe in a loop
pd.read_xml(xml, xpath='.//ChannelData[@channelId="1"]/Values/VT')
"""
t VT
0 2020-08-18T20:30:00 3.2
1 2020-08-18T21:30:00 3.3
2 2020-08-18T22:30:00 3.2
"""
Example of desired output (can contain all or selection of parent attributes), ideally from only reading xml file once
t VT channelId LocationId
0 2020-08-18T20:30:00 3.2 1 123
1 2020-08-18T21:30:00 3.3 1 123
2 2020-08-18T22:30:00 3.2 1 123
3 2020-08-18T20:30:00 21.7 2 123
4 2020-08-18T21:30:00 21.8 2 123
5 2020-08-18T22:30:00 22.0 2 123