Pandas multiindex dataframe - Selecting max from one index within multiindex
Exactly right:
df.groupby(level=0).apply(max)
In my sample DataFrame
:
0Caps Lower A a 0 0.246490 1 -1.265711 2 -0.477415 3 -0.355812 4 -0.724521 b 0 -0.409198 1 -0.062552 2 -0.731789 3 1.131616 4 0.085248B a 0 0.193948 1 2.010710 2 0.289300 3 0.305373 4 1.376965 b 0 0.210522 1 1.431279 2 -0.247171 3 0.899074 4 0.639926
Result:
0Caps A 1.131616B 2.010710
This is how I created the DataFrame
, by the way:
df = pd.DataFrame(np.random.randn(5,4), columns = l)df.columns = pd.MultiIndex.from_tuples(df.columns, names=['Caps','Lower'])df = pd.DataFrame(df.unstack())
Simplier solution is max
only:
#bernie's sample datadf = df.max(level=0)print (df) 0Caps A 1.131616B 2.010710