为了账号安全,请及时绑定邮箱和手机立即绑定

从 XML 数据创建 pandas 数据框

从 XML 数据创建 pandas 数据框

牛魔王的故事 2022-11-01 16:53:32
我正在处理一个 XML 数据文件,其中包含足球比赛期间球员的跟踪数据。查看 XML 数据文件顶部的片段:<?xml version="1.0" encoding="utf-8"?><Tracking update="2017-01-23T14:41:26">  <Match id="2019285" dateMatch="2016-09-13T18:45:00" matchNumber="13">    <Competition id="20159" name="UEFA Champions League 2016/2017" />    <Stadium id="85265" name="Estádio do SL Benfica" pitchLength="10500" pitchWidth="6800" />    <Phases>      <Phase start="2016-09-13T18:45:35.245" end="2016-09-13T19:31:49.09" leftTeamID="50157" />      <Phase start="2016-09-13T19:47:39.336" end="2016-09-13T20:37:10.591" leftTeamID="50147" />    </Phases>    <Frames>      <Frame utc="2016-09-13T18:45:35.272" isBallInPlay="0">        <Objs>          <Obj type="7" id="0" x="-46" y="-2562" z="0" sampling="0" />          <Obj type="0" id="105823" x="939" y="113" sampling="0" />          <Obj type="0" id="250086090" x="1194" y="1425" sampling="0" />          <Obj type="0" id="250080473" x="37" y="2875" sampling="0" />          <Obj type="0" id="250054760" x="329" y="833" sampling="0" />          <Obj type="1" id="98593" x="-978" y="654" sampling="0" />          <Obj type="0" id="250075765" x="1724" y="392" sampling="0" />          <Obj type="1" id="53733" x="-4702" y="45" sampling="0" />          <Obj type="0" id="250101112" x="54" y="1436" sampling="0" />          <Obj type="1" id="250017920" x="-46" y="-2562" sampling="0" />          <Obj type="1" id="105588" x="-1449" y="209" sampling="0" />          <Obj type="1" id="250003757" x="-2395" y="-308" sampling="0" />          <Obj type="1" id="101473" x="-690" y="-644" sampling="0" />          <Obj type="0" id="250075775" x="2069" y="-895" sampling="0" />          <Obj type="1" id="103695" x="-1654" y="-2022" sampling="0" />        </Objs>      </Frame>    </Frames>  </Match></Tracking>
查看完整描述

1 回答

?
呼唤远方

TA贡献1856条经验 获得超11个赞

我使用xml etree模块来遍历 xml 并提取相关数据。注释在下面的代码中以解释该过程:看看它,然后玩代码。希望它适合您的用例


import xml.etree.ElementTree as ET

from collections import defaultdict


d = defaultdict(list)

#since u r reading from a file,

# root should be root = ET.parse('filename.xml').getroot()

#mine is wrapped in a string hence :

 root = ET.fromstring(data)

#required data is in the Frame section

for ent in root.findall('./Match//Frame'):

    #this gets us the timestamp

    Frame = ent.attrib['utc']

    for entry in ent.findall('Objs/Obj'):

        #append the objects to the relevant timestamp

        d[Frame].append(entry.attrib)


df = (pd.concat((pd.DataFrame(value) #create dataframe of the values

                 .assign(Frame=key) #assign keys to the dataframe

                 .filter(['id','Frame','x','y','z']) #keep only required columns

                 for key, value in d.items()),

                axis=1) #concatenate on the columns axis

     )


df.head()


id  Frame   x   y   z   id  Frame   x   y   z

0   0   2016-09-13T18:45:35.272 -46 -2562   0   0   2016-09-13T18:45:35.319 -46 -2558   0

1   105823  2016-09-13T18:45:35.272 939 113 NaN 105823  2016-09-13T18:45:35.319 938 113 NaN

2   250086090   2016-09-13T18:45:35.272 1194    1425    NaN 250086090   2016-09-13T18:45:35.319 1198    1426    NaN

3   250080473   2016-09-13T18:45:35.272 37  2875    NaN 250080473   2016-09-13T18:45:35.319 36  2874    NaN

4   250054760   2016-09-13T18:45:35.272 329 833 NaN 250054760   2016-09-13T18:45:35.319 330 833 NaN



查看完整回答
反对 回复 2022-11-01
  • 1 回答
  • 0 关注
  • 91 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信