【问题标题】:Why do I have empty items in the index of a multiindex为什么我在多索引的索引中有空项目
【发布时间】:2020-11-21 10:27:05
【问题描述】:

这只是为多索引数据帧的初学者分享一个非常基本的概念。

我注意到 2 索引(多索引)df 的索引列中有空项目。虽然这一定是多索引数据帧的基础知识,但我不熟悉它并且忘记了它。我没有很快注意到这可能的意义,因为我有非常大的数字作为索引值,你甚至没有开始检查它们的重要性。使用df.sort_index(inplace=True) 进行排序也无助于摆脱空项目。乍一看,数据集本身的第一个索引部分为空行。搜索“多索引的空项目”也无济于事。 这就是为什么我想与其他多索引数据帧的初学者分享这个非常简单的问题。

这是索引列“A_idx”中的“空项目”:

A_idx B_idx
12344 12345   0.289163 -0.464633 -0.060487
      12345   0.224442  0.177609  2.156436
12346 12346  -0.262329 -0.248384  0.925580
12347 12347   0.051350  0.452014  0.206809
      12348   2.757255 -0.739196  0.183735
      12349  -0.064909 -0.963130  1.364771
12350 12351  -1.330857  1.881588 -0.262170

【问题讨论】:

    标签: python pandas dataframe indexing multi-index


    【解决方案1】:

    这只是为多索引数据帧的初学者分享一个非常基本的概念。

    “空”项是多索引视图的一部分,仅在您输出 df 时出现,它有助于您理解层次结构。如果输出隔离的 Multiindex 类,则没有项目为空。因此,索引项永远不会真正为空,并且“空”字段仅出现在 df 输出中:

    • 如果“A_idx”索引分配给多个“B_idx”索引值,则“A_idx”索引不会重复,因为它是父索引
    • 如果“A_idx”索引在“B_idx”索引重复时指向多个值行,则 B_idx仍会重复,因为它是子项。李>

    如果您使用df.head(10) 并发现“空”索引项在第 1 行中,您也可以使用df.iloc[1].reset_index() 在您的 df 中快速检查这一点。你会看到索引项不为空。

    在下文中,“first”和“second”是索引名称,看似在同一输出行上具有相同的父级权限,但实际上层次结构是从左到右。

    first second
    bar   one     0.289163 -0.464633 -0.060487
          two     0.224442  0.177609  2.156436
    baz   one    -0.262329 -0.248384  0.925580
    foo   one     0.051350  0.452014  0.206809
          two     2.757255 -0.739196  0.183735
          three  -0.064909 -0.963130  1.364771
    qux   one    -1.330857  1.881588 -0.262170
    

    感谢您的示例转到Access last elements of inner multiindex level in pandas dataframe

    这实际上意味着:

    first second
    bar   one     0.289163 -0.464633 -0.060487
    bar   two     0.224442  0.177609  2.156436
    baz   one    -0.262329 -0.248384  0.925580
    foo   one     0.051350  0.452014  0.206809
    foo   two     2.757255 -0.739196  0.183735
    foo   three  -0.064909 -0.963130  1.364771
    qux   one    -1.330857  1.881588 -0.262170
    

    ####

    如何相应地创建层次结构的示例。

    传递给 set_index() 的列列表的顺序以相同的顺序创建层次结构。

    您可以在我从pandas multiindex reindex by rows 借来的一个小示例中查看这一点,其中 df2 覆盖了两个索引的切换。只有 df 显示秘密“空项目”,请参阅 dfdf2 输出:

    df = pd.DataFrame({'month': [1, 4, 7, 10],
                    'year': [2012, 2012, 2013, 2013],
                    'sale': [55, 40, 84, 31]})
    df2 = df.copy()
    
    df=df.set_index(['year','month'])
    df2=df2.set_index(['month','year'])
    

    df:

                sale
    year month      
    2012 1        55
         4        40
    2013 7        84
         10       31
    

    df2:

       month  year  sale
    0      1  2012    55
    1      4  2012    40
    2      7  2013    84
    3     10  2013    31
    
    df.index
    

    输出:

    MultiIndex([(2012,  1),
                (2012,  4),
                (2013,  7),
                (2013, 10)],
               names=['year', 'month'])
    

    或者:

    df2.index
    

    输出:

    MultiIndex([( 1, 2012),
                ( 4, 2012),
                ( 7, 2013),
                (10, 2013)],
               names=['month', 'year'])
    

    查看 df 中的级别:

    df.index.levels[0]
    

    Int64Index([2012, 2013], dtype='int64', name='year')

    df.index.levels[1]
    

    Int64Index([1, 4, 7, 10], dtype='int64', name='month')

    df2.index.levels[0]
    

    Int64Index([1, 4, 7, 10], dtype='int64', name='month')

    df2.index.levels[1]
    

    Int64Index([2012, 2013], dtype='int64', name='year')

    如果您想在输出视图中检查或澄清层次结构的不同级别,请选择一个行并重置索引:

    df.iloc[1].reset_index()
    

    输出:

      index 2012
               4
    0  sale   40
    

    或者:

    df2.iloc[1].reset_index()
    

    输出:

      index    4
            2012
    0  sale   40
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多