How to iterate over MultiIndex levels in Pandas?

Gerenuk picture Gerenuk · Dec 7, 2015 · Viewed 12.1k times · Source

I often have MultiIndex indices and I'd like to iterate over groups where higher level indices are equal. It basically looks like

from random import choice
import pandas as pd
N = 100
df = pd.DataFrame([choice([1, 2, 3]) for _ in range(N)],
                  columns=["A"],
                  index=pd.MultiIndex.from_tuples([(choice("ab"), choice("cd"), choice("de")) 
                                                   for _ in range(N)]))

for idx in zip(df.index.get_level_values(0), df.index.get_level_values(1)):
    df_select = df.ix[idx]

Is there a way to do the for loop iteration more neatly?

Answer

Mzzzzzz picture Mzzzzzz · Dec 7, 2015

Use groupby. The index of the df_select view includes the first two level values, but otherwise is similar to your example.

for idx, df_select in df.groupby(level=[0, 1]):
    ...