def remove_unused_levels(self):
new_levels = []<EOL>new_codes = []<EOL>changed = False<EOL>for lev, level_codes in zip(self.levels, self.codes):<EOL><INDENT>uniques = np.where(np.bincount(level_codes + <NUM_LIT:1>) > <NUM_LIT:0>)[<NUM_LIT:0>] - <NUM_LIT:1><EOL>has_na = int(len(uniques) and (uniques[<NUM_LIT:0>] == -<NUM_LIT:1>))<EOL>if len(uniques) != len(lev) + has_na:<EOL><INDENT>changed = True<EOL>uniques = algos.unique(level_codes)<EOL>if has_na:<EOL><INDENT>na_idx = np.where(uniques == -<NUM_LIT:1>)[<NUM_LIT:0>]<EOL>uniques[[<NUM_LIT:0>, na_idx[<NUM_LIT:0>]]] = uniques[[na_idx[<NUM_LIT:0>], <NUM_LIT:0>]]<EOL><DEDENT>code_mapping = np.zeros(len(lev) + has_na)<EOL>code_mapping[uniques] = np.arange(len(uniques)) - has_na<EOL>level_codes = code_mapping[level_codes]<EOL>lev = lev.take(uniques[has_na:])<EOL><DEDENT>new_levels.append(lev)<EOL>new_codes.append(level_codes)<EOL><DEDENT>result = self._shallow_copy()<EOL>if changed:<EOL><INDENT>result._reset_identity()<EOL>result._set_levels(new_levels, validate=False)<EOL>result._set_codes(new_codes, validate=False)<EOL><DEDENT>return result<EOL>
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.

.. versionadded:: 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]])