How to iterate over MultiIndex levels in Pandas?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 03:27:48

问题


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?


回答1:


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



回答2:


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]


来源:https://stackoverflow.com/questions/34139121/how-to-iterate-over-multiindex-levels-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!