python - 将 XML 数据解析为 pandas 多索引数据帧

标签 python xml pandas xml.etree

我想将 XML 文件中的数据解析为多索引 pandas 数据帧。我的 XML 文件如下所示:

<?xml version="1.0"?>
<catalog>
   <book name="Documents/Books/German">
      <author>Kerstin Gier</author>
      <title>Rubinrot</title>
   </book>
   <book name="Documents/Articles/English">
      <author>Kim Ralls</author>
      <title>Midnight Rain</title>
   </book>
   <book name="Documents/Books/English">
      <author>Eva Corets</author>
      <title>Maeve Ascendant</title>
   </book>
   <book name="Documents/Books/English">
      <author>Karl Parker</author>
      <title>Worldeater</title>
   </book>
</catalog>

目标是将所有图书标签的数据存储到多索引 pandas 数据框中,如下所示:

                              author        title   
Documents  Books     German   Kerstin Gier  Rubinrot        
                     English  Eva Corets    Maeve Ascendant
                              Karl Parker   Worldeater
           Articles  German   Null          Null
                     English  Kim Ralls     Midnight Rain

多索引数据帧的索引应该是属性“name”包含的路径。我不想对任何路径进行硬编码,因为我的现实世界示例有许多不同的路径,并且多索引数据帧将具有 5-6 个维度。

到目前为止我的方法: 我开始创建一个看起来像这样的单个索引数据框

path                        author        title 
Documents/Books/German      Kerstin Gier  Rubinrot
Documents/Articles/English  Kim Ralls     Midnight Rain
Documents/Books/English     Eva Corets    Maeve Ascendant
Documents/Books/English     Karl Parker   Worldeater

问题是:如何将数据帧转换为以路径结构作为索引的多索引数据帧?我看到的问题是更改索引而不丢失与数据的绑定(bind)。

最佳答案

根据您原来的问题和编辑,这是后续解决方案。使用 here 的解析,以及来自 here 的想法:

我们获取现有的路径列,将其拆分为 / 并将其转换为列表,然后使用这些列表值创建新列。

然后我们使用这些列作为新索引。

df

    path                    author          title
0   Documents/Books/German  Kerstin_Gier    Rubinrot
1   Documents/Articles/English  Kim_Ralls   Midnight_Rain
2   Documents/Books/English Eva_Corets  Maeve_Ascendant
3   Documents/Books/English Karl_Parker Worldeater

df[['cat','type','lang']]=pd.DataFrame(df['path'].str.split('/').values.tolist(), index=df.index)

df

    path                    author          title       cat         type    lang
0   Documents/Books/German  Kerstin_Gier    Rubinrot    Documents   Books   German
1   Documents/Articles/English  Kim_Ralls   Midnight_Rain   Documents   Articles    English
2   Documents/Books/English Eva_Corets  Maeve_Ascendant Documents   Books   English
3   Documents/Books/English Karl_Parker Worldeater  Documents   Books   English

df.set_index(['cat','type','lang'])

                                path                    author          title
cat         type        lang            
Documents   Books       German  Documents/Books/German  Kerstin_Gier    Rubinrot
            Articles    English Documents/Articles/English  Kim_Ralls   Midnight_Rain
            Books       English Documents/Books/English Eva_Corets  Maeve_Ascendant
                        English     Documents/Books/English Karl_Parker Worldeater

从那里,显然您可以根据需要删除路径

关于python - 将 XML 数据解析为 pandas 多索引数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55365328/

相关文章:

python - Pandas 数据帧 : Writing values to column depending on a value check of existing column

java - Android Studio/Java/XML 中的 "Unbound Prefix"是什么?

Java通过替换复制xml节点和子节点

android - 如何用android :digits and android:imeOptions ="actionDone" work properly in Android制作xml

python - Pandas Dataframe Resample OHLC 开盘价错误

python - 使用 python pandas 将具有多行的 python 数据框转换为一行?

python - 枚举列表的列表和排序

C# .NET 串行端口连接,不读取或写入

python - python的命令行界面

python - Python Pandas TypeError : first argument must be string or compiled pattern