繁体   English   中英

将 XML 数据解析为 Pandas 多索引数据帧

[英]Parsing XML data into pandas multiindex dataframe

我想将 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>

目标是将所有书籍标签中的数据存储到多索引熊猫数据帧中,该数据帧应如下所示:

                              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

问题是:如何将数据帧转换为以路径结构作为索引的多索引数据帧? 我看到的问题是在不丢失数据绑定的情况下更改索引。

根据您的原始问题和编辑,这是后续解决方案。 使用来自解析这里,并从思想在这里

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM