为什么我会在切片数据框中看到所有原始索引元素? [英] Why do I see all original index elements in a sliced dataframe?

查看:216
本文介绍了为什么我会在切片数据框中看到所有原始索引元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的多索引数据框:

I have a multiindex dataframe like this:

import pandas as pd
import numpy as np


df = pd.DataFrame({'ind1': list('aaaaaaaaabbbbbbbbb'),
                   'ind2': list('cccdddeeecccdddeee'),
                   'ind3': list(range(3))*6,
                   'val1': list(range(100, 118)),
                   'val2': list(range(70, 88))})

df_mult = df.set_index(['ind1', 'ind2', 'ind3'])

                val1  val2
ind1 ind2 ind3            
a    c    0      100    70
          1      101    71
          2      102    72
     d    0      103    73
          1      104    74
          2      105    75
     e    0      106    76
          1      107    77
          2      108    78
b    c    0      109    79
          1      110    80
          2      111    81
     d    0      112    82
          1      113    83
          2      114    84
     e    0      115    85
          1      116    86
          2      117    87

我现在可以使用 .loc 选择它的一个子集

I can now select a subset of it using .loc like this

df_subs = df_mult.loc[pd.IndexSlice['a', ['c', 'd'], :], :]

给出预期的

                val1  val2
ind1 ind2 ind3            
a    c    0      100    70
          1      101    71
          2      102    72
     d    0      103    73
          1      104    74
          2      105    75

当我打印

df_subs.index

我得到

MultiIndex(levels=[[u'a', u'b'], [u'c', u'd', u'e'], [0, 1, 2]],
           labels=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 1, 1], [0, 1, 2, 0, 1, 2]],
           names=[u'ind1', u'ind2', u'ind3'])

为什么0级还有 b 而不只是 a

Why is there still b in level 0 and not just a?

如果我想将索引的元素用于其他内容,这可能会成为一个问题。那么

This could become an issue if I want to use the elements of the index for something else. Then

df_subs.index.levels[0]

给我

Index([u'a', u'b'], dtype='object', name=u'ind1')

然而,

df_subs.index.get_level_values('ind1').unique()

给我

Index([u'a'], dtype='object', name=u'ind1')

看起来与我不一致。

这是一个错误还是预期的行为?

Is this a bug or intended behavior?

推荐答案

有关于GitHub周围的讨论此行为此处

There's a discussion on GitHub surrounding this behavior here.

简而言之,您看到的级别不是根据您实际观察到的MultiIndex中的值计算的 - 在您首次设置MultiIndex之后,未观察到的级别将通过索引保持不变。这允许在所有视图和一些MultiIndex的副本之间共享级别索引,这在内存方面很好 - 即, df_mult df_subs 在内存中共享相同的基础级别索引。

In short, the levels you see are not computed from the values in the MultiIndex that you actually observe - unobserved levels will persist through indexing after you first set up the MultiIndex. This allows the level indexes to be shared between all the views and copies of some MultiIndex, which is nice memory-wise - i.e., df_mult and df_subs are sharing the same underlying level indexes in memory.

如果您想要重新计算级别以消除未使用的级别并创建新的MultiIndex,则可以使用 MultiIndex.remove_unused_levels()

If you have a case for which you want to recompute the levels to get rid of the unused ones and create a new MultiIndex, you can use MultiIndex.remove_unused_levels().

在您的情况下

>>> df_subs.index.remove_unused_levels().levels[0]
Index(['a'], dtype='object', name='ind1')

这篇关于为什么我会在切片数据框中看到所有原始索引元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆