【问题标题】:Pivoting XML data in Pandas with tags as headers of pandas dataframe在 Pandas 中使用标签作为 pandas 数据帧的标头来透视 XML 数据
【发布时间】:2020-01-17 10:04:21
【问题描述】:

我已将 XML 数据转换为 pandas 数据框。现在我希望旋转数据以达到我理想的 df 输出。请帮帮我。

我目前的外出:

       data                name
0      Aruba               Country or Area
1      Population, total   Item
2      1960                Year
3      54211               Value
4      Aruba               Country or Area
5      Population, total   Item
6      1961                Year
7      55438               Value
8      Aruba               Country or Area
9      Population, total   Item
10     1962                Year
11     56225               Value
12     Aruba               Country or Area
13     Population, total   Item
14     1963                Year
15     56695               Value
16     Aruba               Country or Area
17     Population, total   Item
18     1964                Year
19     57032               Value

我正在直接粘贴我用来旋转的最后一行。

xml_df = xml_df.pivot(index='data', columns='name')

期望的输出:

Country or Area   Year     Item                 Value
Aruba             1960     Population, total    54211
Aruba             1961     Population, total    55348

等等……

【问题讨论】:

    标签: python python-3.x pandas python-2.7 pandas-groupby


    【解决方案1】:

    IIUC,你可以在namecumcount 上尝试groupby,然后是unstack

    df.assign(k=df.groupby('name').cumcount()).set_index(['k','name']).unstack()
    

                    data                                
    name Country or Area               Item  Value  Year
    k                                                   
    0              Aruba  Population, total  54211  1960
    1              Aruba  Population, total  55438  1961
    2              Aruba  Population, total  56225  1962
    3              Aruba  Population, total  56695  1963
    4              Aruba  Population, total  57032  1964
    

    详情: cumcount()

    df.groupby('name').cumcount()
    

    这按名称和Numbers each item in each group from 0 to the length of that group - 1 分组,并使用df.assign() 我们为数据框分配一个新列k。然后使用set_index() wee 将名称和k 列设置为索引,这样您就可以得到:

    print(df.assign(k=df.groupby('name').cumcount()).set_index(['k','name']))
                                    data
    k name                              
    0 Country or Area              Aruba
      Item             Population, total
      Year                          1960
      Value                        54211
    1 Country or Area              Aruba
      Item             Population, total
      Year                          1961
      Value                        55438
    2 Country or Area              Aruba
      Item             Population, total
      Year                          1962
      Value                        56225
    .......
    .....
    

    使用此数据,我们使用unstack(),这有助于“旋转(必须是分层的)索引标签的级别,返回具有新级别的列标签的 DataFrame,其最内层由旋转的索引标签组成" 因此,这会将索引的最后一级(默认情况下)转换为我们需要的列。

    【讨论】:

    • 您能否简要解释一下 cumcount 和 unstack 如何避免使用技术术语?
    • @KaustubhUrsekar 当然,请允许我一些时间,我会用解释更新答案
    【解决方案2】:

    枢轴的另一种方式:

    df['idx'] = df.name.eq('Country or Area').cumsum()
    df.pivot(index='idx', columns='name', values='data')
    

    输出:

    name Country or Area               Item  Value  Year
    idx                                                 
    1              Aruba  Population, total  54211  1960
    2              Aruba  Population, total  55438  1961
    3              Aruba  Population, total  56225  1962
    4              Aruba  Population, total  56695  1963
    5              Aruba  Population, total  57032  1964
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-15
      • 2021-08-28
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      • 2020-11-11
      • 1970-01-01
      相关资源
      最近更新 更多