pandas.MultiIndex.remove_unused_levels¶
-
MultiIndex.
remove_unused_levels
()[source]¶ Create a new MultiIndex from the current that removes unused levels, meaning that they are not expressed in the labels.
The resulting MultiIndex will have the same outward appearance, meaning the same .values and ordering. It will also be .equals() to the original.
New in version 0.20.0.
Returns: - MultiIndex
Examples
>>> i = pd.MultiIndex.from_product([range(2), list('ab')]) MultiIndex(levels=[[0, 1], ['a', 'b']], codes=[[0, 0, 1, 1], [0, 1, 0, 1]])
>>> i[2:] MultiIndex(levels=[[0, 1], ['a', 'b']], codes=[[1, 1], [0, 1]])
The 0 from the first level is not represented and can be removed
>>> i[2:].remove_unused_levels() MultiIndex(levels=[[1], ['a', 'b']], codes=[[0, 0], [0, 1]])