Pandas: Get unique MultiIndex level values by label

Pandas 0.23.0 finally introduced a much cleaner solution to this problem: the level argument to Index.unique():

In [3]: df.index.unique(level="country")
Out[3]: Index(['DE', 'FR'], dtype="object", name="country")

This is now the recommended solution. It is far more efficient because it avoids creating a complete representation of the level values in memory, and re-scanning it.

Leave a Comment