Reputation: 3048
I have the following data:
arrays = [['bar', 'bar', 'baz', 'baz'],
['one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
s = pd.Series(np.random.randn(4), index=index)
first second
bar one 1.791849
two 0.334121
baz one -0.655277
two -1.296491
Now I filter on the index and remove "one":
t = s[s.index.get_level_values(1) != "one"]
first second
bar two 0.334121
baz two -1.296491
The strange thing is that "one" still shows up in the index (dependent on the method I use to access index values):
t.index.levels[1]
Index(['one', 'two'], dtype='object', name='second')
Or:
t.index.get_level_values(1)
Index(['two', 'two'], dtype='object', name='second')
Would you have any idea why "one" is still showing in the index?
Upvotes: 3
Views: 175
Reputation: 862921
Use MultiIndex.remove_unused_levels
, because by default after filtering is index not changed, I guess reason is performance:
print (t.index.remove_unused_levels())
MultiIndex([('bar', 'two'),
('baz', 'two')],
names=['first', 'second'])
print (t.index.remove_unused_levels().levels[1])
Index(['two'], dtype='object', name='second')
Upvotes: 4