How to iterate over MultiIndex levels in Pandas?

被刻印的时光 ゝ 提交于 2019-12-04 08:24:49

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

Alternatively to groupby logic you can use a lambda function, which has the advantage of not having to specify the number of levels, i.e. it will pick all levels except the very last one:

for idx in df.index.map(lambda x: x[:-1]):
 df_select=df.ix[idx]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!