You can (as @unutbu suggested) use a hierarchical index, but when you have a three-dimensional array, you should consider using the pandas Panel . "Especially when one of the dimensions is time, as in this case.
The panel is often ignored, but in the end it comes from the name pandas. (Panel Data System or something like that).
The data is slightly different from your original, so there are no two sizes with the same length:
df1 = pd.DataFrame([[1, 0], [1, 2], [2, 0], [2, 3]], columns=['a', 'b']) df2 = df1 + 1 df3 = df1 + 10
Panels can be created in several ways, but one of them. You can create a dict from your index and dataframes with:
s = pd.Panel(dict(zip(idx,[df1,df2,df3])))
The average value you are looking for is just working with the right axis (in this case axis = 0):
s.mean(axis=0) Out[80]: ab 0 4.666667 3.666667 1 4.666667 5.666667 2 5.666667 3.666667 3 5.666667 6.666667
With your data, sum(axis=0) returns the expected result.
EDIT: OK is too late for panels, because the hierarchical index approach has already been adopted. I will say that this approach is preferable if the data is known as โdanglingโ with an unknown but different number in each group. For square data, the panel is the absolute way to go and will be significantly faster with more built-in operations. pandas 0.15 has many improvements for multi-level indexing, but still has limitations and dark edges in real applications.
Phil cooper
source share