how do i perform complicated df action for all rows of a multilevel index dataframe?

You can change foo function for integer division by helper array by length of DataFrame, last add group_keys=False for avoid duplcicated first level of MultiIndex:

def foo(df):
    res = (df.apply(lambda x: row_reduce(x[0], x[1]), axis=1)
                   .groupby(np.arange(len(df)) // 2)
    return res

df = df.groupby(level=0, group_keys=False).apply(foo)
print (df)
i0  i1
0   0      05,24
    1      05,24
1   2      43,62
    3      43,62
    4     81,100
    5     81,100
dtype: object

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top